0
當我嘗試將當前級別的計數保存到applicationWillTerminate
函數中的NSUserDefaults時,它崩潰在我身上。爲什麼?當試圖保存到NSUserDefaults時崩潰
----------------------- AppDelegate.m -------------------- ---
- (void)applicationWillTerminate:(UIApplication *)application {
CCDirector *director = [CCDirector sharedDirector];
HelloWorldLayer *hWClass = [[HelloWorldLayer alloc]init];
NSNumber *lCount = [NSNumber numberWithInt:hWClass -> level];
hWClass -> lvlCount =[NSUserDefaults standardUserDefaults];
[hWClass -> lvlCount setObject:lCount forKey:@"levelCount"];
[[director openGLView] removeFromSuperview];
[viewController release];
[window release];
[director end];
}
----------------------- HelloWorldLayer.h -------------- ---------
...{
@public
int level;
NSUserDefaults *lvlCount;
int health;
...
}
@property (nonatomic,retain) NSUserDefaults *lvlCount;
----------------------- HelloWorldLayer.m -------- ---------------
NSNumber *currentLevel = [[NSUserDefaults standardUserDefaults] objectForKey:@"levelCount"];
if (currentLevel != nil){
int cLevel = [currentLevel integerValue];
[self generateLevelFromPlist:cLevel];
}else{
[self generateLevelFromPlist:1];
}
沒有爲我工作。終止時仍然崩潰。謝謝你的方式... –