尝试保存到 NSUserDefaults 时崩溃

发布于 2024-12-20 15:45:57 字数 1477 浏览 1 评论 0原文

当我尝试将当前级别计数保存到 applicationWillTerminate 函数中的 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];
        }

When I try to save the current level count to NSUserDefaults in the applicationWillTerminate function it crashes on me. Why ?

----------------------- 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];
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反目相谮 2024-12-27 15:45:57

您可以使用 -> 直接访问实例变量,然后为其分配一个自动释放的值。您需要使用点语法访问该属性才能正确保留它。

   hWClass.lvlCount =[NSUserDefaults standardUserDefaults];
   [hWClass.lvlCount setObject:lCount  forKey:@"levelCount"];

另外,通过删除 @public 或将其更改为 @private 将以下变量声明更改为私有,这样您就不会再遇到该问题了。

    ...{
    @private
    int level;
    NSUserDefaults *lvlCount;
    int health;
    ...
    }

You are accessing the instance variable directly by using -> and then assigning it an autoreleased value. You need to access the property using dot syntax for it to properly be retained.

   hWClass.lvlCount =[NSUserDefaults standardUserDefaults];
   [hWClass.lvlCount setObject:lCount  forKey:@"levelCount"];

Also change the following variable declarations to private by remove @public or changing it to @private so you do not have that issue anymore.

    ...{
    @private
    int level;
    NSUserDefaults *lvlCount;
    int health;
    ...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文