【发布时间】:2010-07-13 21:36:20
【问题描述】:
这是一个关于使用 Cocoa 数组时的内存管理和最佳实践的一般问题。
以下哪个“更好”:
NSArray *pageControllers = [[NSArray alloc] initWithObjects:
[[Page1 alloc] initWithNibName:@"Page1" bundle:nil],
[[Page2 alloc] initWithNibName:@"Page2" bundle:nil],
[[Page3 alloc] initWithNibName:@"Page3" bundle:nil],
nil];
...then release NSArray later when not needed anymore...
或者
NSMutableArray *pageControllers = [[NSMutableArray alloc] init];
UIViewController *page1 = [[Page1 alloc] initWithNibName:@"Page1" bundle:nil];
[pageControllers addObject:page1];
[page1 release];
UIViewController *page2 = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil];
[pageControllers addObject:page2];
[page2 release];
UIViewController *page3 = [[Page3 alloc] initWithNibName:@"Page3" bundle:nil];
[pageControllers addObject:page3];
[page3 release];
...then release NSMutableArray later when not needed anymore...
或者还有什么更好的?
【问题讨论】:
标签: objective-c memory-management nsmutablearray nsarray