Correct way to declare an NSArray (Memory-Wise)
I have an NSArray which has the properties nonatomic and retain. What I would like to know is am I initialising it in the correct way so that the retain count is what it should be?
The _lengthArr is the @synthesized ivar of lengthArr in my.h
@synthesize lengthArr = _lengthArr;
_lengthArr = [[NSArray arrayWithObjects:@"10 Minutes", @"20 Minutes", @"30 Minutes", @"1 Hour", @"2 Hours", @"5 Hours", @"5 Hours +", nil] retain];开发者_StackOverflow中文版
You are assigning to an ivar there, so the code you posted will do what you want. You could also use _lengthArr = [[NSArray alloc] initWithObjects:...]
, which would avoid a useless autorelease.
Or you could do self.lengthArr = [NSArray arrayWithObjects:...]
to allow the setter to automatically retain it, although it is recommended to avoid this in initialization methods.
精彩评论