1
這是關於使用Cocoa數組時的內存管理和最佳實踐的一般問題。填充NSArray/NSMutableArray,正確的方法
下列哪項是 「更好」:
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...
或者是有別的,甚至更好?
謝謝。那麼我會繼續第二個例子。 – RyJ 2010-07-13 21:58:14
那麼,如果你記得自動釋放頁面或者將它們的指針存儲到一個變量中,並且在創建數組之後調用'release',那麼第一個示例將不會泄漏。一般來說,對於簡單的,非動態的東西,我會去不可變的路線,但它確實無關緊要。 – Wevah 2010-07-14 02:36:57