Search code examples
objective-cmemory-managementpropertiesretain

When to use self and when to use retain


am working my way through the "Beginning iPad Development" Apress book and have noticed that sometimes when the author is assigning values to a property they will use:

self.variable = value;

and other times, they will use:

variable = [value retain];

In both cases variable is a property defined as:

@property (nonatomic, retain) TYPE variable;

I wondered if anyone knew why this is done to help me better understand

Thanks, William


Solution

  • They are often equivalent memory-wise. The compiler turns self.variable = value into [self setVariable:value] which then calls the generated setter (if you're using @synthesize) and retains it for you.