NSUserDefaults standardUserDefaults synchronize给出错误的结果目标c

时间:2016-02-29 16:14:29

标签: ios objective-c nsstring nsuserdefaults

我想用高分保存 synchronize  在我的应用程序中,但我得到了胡言乱语的号码。这就是我得到的:
GameScene.m

    if (highScore <score) {
    highScore = score;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:highScore forKey:@"highScore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
  }

GameOver.m

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
    NSString *rezult =[prefs stringForKey:@"highScore"];

    SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
    highscorelabel.text =[NSString stringWithFormat:@"%ld",(long)rezult];
        highscorelabel.position = CGPointMake(CGRectGetMidX(self.frame), 280);
        highscorelabel.fontSize = 45;
        highscorelabel.zPosition = 5;
    [self addChild:highscorelabel];

为什么我的错误值以及如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

由于rezultNSString,您收到的错误值,您应该更改此内容:

NSString *rezult =[prefs stringForKey:@"highScore"];

到此:

NSInteger rezult = [prefs integerForKey:@"highScore"];

答案 1 :(得分:-1)

每次使用时都尝试不启动另一个nsuserdefaults [[NSUserDefaults standardUserDefaults] setValue:surname.text forKey:@“Nume”];来写 [[NSUserDefaults standardUserDefaults] valueForKey:@“Name”]阅读

并确认您使用相同的对象类型!