2012-08-07 90 views
0

這裏是我的問題:ViewDidLoad方法不保留一個變量?

viewDidLoad方法創建使用NSUserDefaults一個變量(如果它的「第一跑」創建它,並NSNumber填充它。然後我嘗試另一種方法來使用它,。什麼都沒有。它看起來像它的空。任何人都可以幫我嗎?地塊由於

- (void)viewDidLoad { 

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; 

if ([defaults objectForKey:@"seriesBool"]!=nil) 

{ 
    seriesBool = [defaults objectForKey:@"seriesBool"]; 

} 

else 

{ 
    int i; 

    seriesBool = [NSMutableArray arrayWithCapacity:9]; 

    for(i=0; i<9; i++) 

    { 
     [seriesBool addObject:[NSNumber numberWithBool:YES]]; 

    } 

} 


-(IBAction)toAction:(id)sender 
{ 
NSLog(@"array: %@", seriesBool); 
} 

seriesBool是空的......

+0

您是否使用ARC或MRR? – trojanfoe 2012-08-07 12:18:25

+0

我認爲在這種情況下並不重要。由於他沒有使用強大的財產,也沒有保留iVar,所以iVar正在被釋放。 – 2012-08-07 12:21:52

+0

@BrunoKoga你怎麼知道他沒有使用'strong'? – trojanfoe 2012-08-07 12:22:24

回答

1

你需要學習我的第一個基礎管理。基本上,發生了什麼事情,你不保留系列滾輪iVar。

到這裏看看:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

儘量保留你的伊娃,或更好,營造濃厚的/保留屬性,並使用存取方法。

+0

是的。我沒有保留它。我認爲當你在接口中編寫屬性時:@property(nonatomic,retain)NSMutableArray * seriesBool;它已經保留了。但事實並非如此。感謝所有的答案。我會先學習這個指南;) – user1581885 2012-08-07 12:52:19

3

必須設置屬性等

@property(nonatomic,retain) NSMutableArray * seriesBool; 

,並用它

self.seriesBool = [NSMutableArray的arrayWithCapacity:9];

使用

seriesBool = [[NSMutableArray alloc] initWithCapacity:9]; 

,而不是

seriesBool = [NSMutableArray arrayWithCapacity:9]; 

& u必須alloc和分配對象

seriesBool = [[NSMutableArray alloc] init]; 
seriesBool = [defaults objectForKey:@"seriesBool"];