Given the following code
@interface MyClass
{
SomeObject* o;
}
@property (nonatomic, retain) SomeObject* o;
@implementation MyClass
@synthesize o;
- (id)initWithSomeObject:(SomeObject*)s
{
if (self = [super init])
{
o = [s retain]; // WHAT DOES THIS DO? Double retain??
}
return self
}
@end
It is not a double retain;
swill only be retained once.The reason is that you’re not invoking the synthesized setter method within your initializer. This line:
retains
sand setsoto be equal tos; that is,oandspoint to the same object. The synthesized accessor is never invoked; you could get rid of the@propertyand@synthesizelines completely.If that line were:
or equivalently
then the synthesized accessor would be invoked, which would retain the value a second time. Note that it generally not recommended to use accessors within initializers, so
o = [s retain];is the more common usage when coding aninitfunction.