开发者

Correct way to declare an NSArray (Memory-Wise)

开发者 https://www.devze.com 2023-03-07 23:33 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消