Custom setter and getter method implementations

For non-atomic, retain property:

  
- (void)setObject:(anObject *)theObject{
      if (object != theObject) { //make you are not attempting to set the same object as is already set
            [object release];
            object = [theObject retain];
      }
}
  
- (anObject *)object{

    anObject *objectToBeReturned = [object retain];
    return [objectToBeReturned autorelease];
}

For non-atomic, copy:

  
- (void)setObject:(anObject *)theObject{
      if (object != theObject) { //make you are not attempting to set the same object as is already set
            [object release];
            object = [theObject copy];
      }
}
  
- (anObject *)object{

    anObject *objectToBeReturned = [object retain];
    return [objectToBeReturned autorelease];
}

For assign:

  
- (void)setObject:(anObject *)theObject{
      if (object != theObject) { //make you are not attempting to set the same object as is already set
            object = theObject;
      }
}
  
- (anObject *)object{
    return objectToBeReturned
}

For atomic, retain:

Setter:


- (void)setObject:(anObject *)theObject{

    @synchronized(self) {//don't allow any other updates to occur until finished
        if (object != theObject) { //make you are not attempting to set the same object as is already set
            [object release];
            object = [theObject retain];
        }
    }
}

Getter:

  
- (anObject *)object{

    @synchronized(self) {
        id objectToBeReturned = [object retain];
    }

    return [objectToBeReturned autorelease];
}

Please note if KVO automatic notification is turned off in the above setter methods you will have to surround object = [theObject retain]; with:

  
[self willChangeValueForKey:@"object"];
object = [theObject retain];  
[self didChangeValueForKey:@"object"];