Possible Duplicate:
Difference between self.ivar and ivar?
I recently had a problem where I was trying to initialize an object, which involved passing in an NSMutableArray for assignment.
I tried doing
- (id)initWithFrame:(CGRect)frame menus:(NSMutableArray *)aMenusArray view:(UIView*)gView
{
self = [super initWithFrame:frame];
if (self) {
// ...some code
_menusArray = [aMenusArray retain]; // This works
// _menusArray = aMenusArray; This does not work.
// self.menusArray = [aMenusArray retain]; This does not work.
// self.menusArray = aMenusArray; This does not work.
// ...some code
}
return self;
}
"Does not work" means that when I later attempt to treat the array as a NSMutableArray and pass [self.menusArray removeObjectAtIndex:0] or something like that, it doesn't cause an exception with unrecognized selector used on __NSArrayI. In other words, the "does not work" causes the self.menusArray to become an Immutable Array instead of a mutable one.
I was wondering why _menusArray = [aMenusArray retain] works and why the others don't. As far as I knew, property and synthesize simply create accessor methods (the getters and setters). I asked my colleagues, to which they replied I probably have some corrupted memory somewhere.
To be clear, aMenusArray is declared as an NSMutableArray and _menusArray is declared in the header file as follows:
NSMutableArray *_menusArray;
and its properties and synthesize (which is in the implementation) are:
@property (nonatomic, retain) NSMutableArray *menusArray;
@synthesize menusArray = _menusArray;