开发者

Iphone Allocate memory in initWithNibName

Is it c开发者_如何学运维orrect to allocate memory for an array that is set as a property in the method initWithNibName if i don't want to allocate memory for it anymore (even if the view is pop and then is pushed again)?

Thanks


Well, if a view is shown and then hidden, it will receive both an initWithNibName and then a release; so what you should do is something like this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   yourArray = [[NSArray alloc] init];
}

- (void) dealloc
{
   [yourArray release];
   [super dealloc];
}

No matter how short the view controller's life, it will nonetheless receive a release upon being dismissed, which in turn will eventually result in dealloc being called.


Its is correct, only make sure to release it in your dealloc. make sure that when you alloc initing you DO NOT use your setter, instead you should alloc init the instance variable directly:

_myArray = [[NSArray alloc]init];

if you want to go only through the property method then do something like this:

self.myArray = [NSArray array];

and again, dont forget to release it in your dealloc:

 -(void)dealloc
{
    [_myArray release];
    [super dealloc];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜