setter and getter for an atomic property

tom picture tom · Dec 5, 2011 · Viewed 9.1k times · Source

what's the auto-gen'd getter and setter look like for the following property value?

... in .h
@interface MyClass : NSObject {
@private
    NSString *_value;
}

@property(retain) NSString *value;

... in .m
@synthesize value = _value;

what if I change the property to be

@property(retain, readonly) NSString *value;

specifically I am interested in the atomic part of the story, plus the retain, and if possible, detailed code would be more clear as to what's going exactly going on behind the scene.

Answer

zpasternack picture zpasternack · Dec 5, 2011

They would look something like:

- (NSString*) value 
{
    @synchronized(self) {
        return [[_value retain] autorelease];
    }
}

- (void) setValue:(NSString*)aValue
{
    @synchronized(self) {
        [aValue retain];
        [_value release];
        _value = aValue;
    }
}

If you change the property to readonly, no setter is generated. The getter will be identical.