如何在目标c中加载AppDelegate中的NSUserDefaults standardUserDefaults

时间:2016-03-04 13:16:42

标签: ios objective-c nsuserdefaults appdelegate

我不明白如何正确使用NSUserDefaults standardUserDefaults。我有GameOver.m个文件,如果得分高,则会检查得分,如果是,它会像这样保存:

if(score > highScore){
  highScore =score;
  bestScore = highScore;

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  [prefs setInteger:bestScore forKey:@"bestScore"];
  [prefs synchronize];
}

要加载高分我使用:

NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
NSInteger theHighScore =[prefs integerForKey:@"bestScore"];

它可以工作,但是在关闭应用程序并重新打开高分后,我读到,加载过程需要在- (void)applicationDidEnterBackground:(UIApplication *)application的AppDelegate文件中,我该怎么做?

修改
GameScene.m

-(void)Scoring {
  NSUserDefaults *scoreprefs = [NSUserDefaults standardUserDefaults];
  [scoreprefs setInteger:score forKey:@"score"];
}

GameScene.m

#import "GameScene.h"
@implementation GameOver
NSInteger score;
NSInteger highScore;
NSInteger bestScore;

- (id)initWithSize:(CGSize)size playerLose:(BOOL)isLose {
  self = [super initWithSize:size];
  if (self) {
    NSUserDefaults *scoreprefs =[NSUserDefaults standardUserDefaults];
    NSInteger score = [scoreprefs integerForKey:@"score"];

    SKLabelNode *scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
    scorelabel.text =[NSString stringWithFormat:@"%ld",(long)score];
    // then i define position/fontsize for scoreLabel
    [self addChild:scorelabel];

    if(score > highScore){
      highScore =score;
      bestScore = highScore;

      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
      [prefs setInteger:bestScore forKey:@"bestScore"];
      [prefs synchronize];
    }

    NSUserDefaults *prefs =[NSUserDefaults standardUserDefaults];
    NSInteger theHighScore =[prefs integerForKey:@"bestScore"];
    SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
    highscorelabel.text =[NSString stringWithFormat:@"%ld",(long)theHighScore];
    //define position/fontsize for highscorelabel
  }
  return self;
}

就是这样,它。

2 个答案:

答案 0 :(得分:1)

您没有获取高分数据。因此,以下条件每次都很好,因为highScore变量将为0.在此阶段,即使bestScore大于{{1},bestScore也会被当前分数覆盖}}

score

答案 1 :(得分:1)

@interface GameOver ()
@property (nonatomic, assign) NSInteger score;
@property (nonatomic, assign) NSInteger highScore;
@property (nonatomic, assign) NSInteger bestScore;
@end

@implementation GameOver
@synthesize score,bestScore,highScore;

- (id)initWithSize:(CGSize)size playerLose:(BOOL)isLose {
    NSUserDefaults *scoreprefs =[NSUserDefaults standardUserDefaults];

    if (self = [super initWithSize:size]) {
        if (self) {

            score = [scoreprefs integerForKey:@"score"];
            highScore = [scoreprefs integerForKey:@"bestScore"];

            SKLabelNode *scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
            scorelabel.text =[NSString stringWithFormat:@"%ld",(long)score];
            // then i define position/fontsize for scoreLabel
            [self addChild:scorelabel];

            if(score > highScore){
                highScore =score;
                bestScore = highScore;

                [scoreprefs setInteger:bestScore forKey:@"bestScore"];
                [scoreprefs synchronize];
            }

            SKLabelNode *highscorelabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo-Bold"];
            highscorelabel.text =[NSString stringWithFormat:@"%ld",(long)highScore];
            //define position/fontsize for highscorelabel
        }

    }
        return self;
}

问题是你没有初始化你的HighScore,并且检查

score(assume 5242)>highScore(nil)

永远是真的

EDITED