I have property:
@property(nonatomic, retain) NSMutableArray *myvar;
First case:
myvar = [[NSMutableArray alloc] init];
NSLog(@retainCount: %i:", [myvar retainCount]);
outputs:
retainCount: 1
Second case:
self.myvar = [[NSMutableArray alloc] init];
NSLog(@retainCount: %i:", [self.myvar retainCount]);
outputs:
retainCount: 2
My question is: why in the second case retain value is 2 ?
+1 because you
alloc/init‘d it+1 because
self.myvarretains it (as set out in your property declaration)if you
autoreleaseafter alloc/init, it will go back down to 1… then if you setself.myvarto nil, it will hit 0 (if nothing else has retained it in the meantime)But as vikingosegundo has said, you don’t want to be messing with retain counts. The OS determines when to knock them back down, so you can’t use them as a reliable measure of state.