In my .h file
I have:
NSMutableArray *myArray;
@property (nonatomic, retain) NSMutableArray *myArray;
My .m file
looks basically like this:
@synthesize myArray;
- (id) init {
self = [super init];
if (self != nil)
{
self.myArray = .... ? // here I want to create an empty array
}
return self;
}
- (void) dealloc {
[self.myArray release];
[super dealloc];
}
What I'm not sure about is what do to in the init
.
1)
self.myArray = [[NSMutableArray alloc] init];
2)
NSMutableArray *tmp = [[NSMutableArray alloc] init];
self.myArray = tmp;
[tmp release];
Solution 1 doesn't seem right to me, because of my @property (retain)
setting I automatically increase the retain counter when setting self.myArray, but additionally I have already a "+1 retain" due to the [NSMutableArray alloc]
and then ending up with a retain count of 2 for that object, but only releasing once in the dealloc
. Thus the second solution seems more correct to me, even though it is cumbersome.
Also am I wondering if self.myArray = ...
is actually the same as [self setMyArray:...]
and thus does increase the retain count.
UPDATE
I actually found the answers (and even more details) here in case anyone is interested in reading more.