I’m still pretty new to Objective-C coding (as evidenced by this question) and I think I’m not completely understanding how using the retain attribute in a @property declaration works.
Here is an example class:
@interface Foo : NSObject {
NSMutableArray *myArray;
}
@property (retain) NSMutableArray *myArray;
My understanding was that adding the retain attribute to the @property declaration (and using the necessary @synthesize delcaration in the implementation file) will basically do the following setter and getter for me:
- (void)setMyArray:(NSMutableArray *)newArray {
myArray = [[NSMutableArray alloc] initWithArray:newArray];
[newArray release];
}
- (NSMutableArray *)myArray {
return myArray;
}
Is this accurate or am I mistaken on how the retain attribute works?
Adding the retain attribute will actually generate this code:
The reason the retain method is called on newArray before release on the old value is that if newArray and myArray are the same object, the array will be released before it is retained again.