In a synthesized property setter, the code does something roughly analogous to this (we'll have name
be the property name, and _name
will be the instance variable to which it refers):
- (void) setName: (NSString *) newValue
{
if ( newValue == _name )
return;
[newValue retain];
[_name release];
_name = newValue;
}
Now, that shows what would happen based on your property declaration above. Depending on the other possible attributes of that property, that function would either use [newValue copy]
instead of [newValue retain]
(if the 'copy' attribute was specified), or it would all be wrapped in an @synchronized(self)
block (if the 'nonatomic' attribute was NOT supplied).
I should also note that since your property refers to an NSString, which implements the NSCopying protocol, you should really be using copy rather than retain, i.e.:
@property (nonatomic, copy) NSString * name;
That will cause the synthesized setter to use -copy
instead of -retain
, which is safer when you're actually passed an NSMutableString object. An immutable string would ultimately only get retained by the -copy
call, while a mutable string would create a new immutable copy. Failing to do that means that the string itself could be changed after it's been passed into the setter, changing the value without your object's knowledge.