Betkowski
Betkowski

Reputation: 491

Swift: HighScore / NSUserDefaults

I have a problem with NSUserDefaults.

I followed Steve's answer from this one: Saving highscores with NSUserDefaults

And now, I have a problem. When my GameScene is to be over, and just before the transfer to the GameOverScene is initialized, I do this:

    if scene.score > Score.highScore {
        var defaults = NSUserDefaults()
        defaults.setInteger(scene.score, forKey: "highScore")
        println("score1, highscore1")
        println(scene.score)
        println(Score.highScore)
    }

And you can see I compare the gameScene score to the highscore, and then set the highscore's Integer to the scene's one, that player achieved. Yet, program print's out the current score correctly, leaving the highScore the same, at 0. Before I tried to do:

Score.HighScore = scene.score // changing Score.HighScore to the scene.score manually. 

Unfortunately, did not work. Here's the full HighScore Class, that handles saving and retrieving highscore: http://pastebin.com/dz3b3WAk

Any help appreciated.

EDIT: or maybe, someone is aware of easier solution of saving something to NSUserDefaults, without all that complication?

Upvotes: 0

Views: 579

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Steve's answer in that related question does not use NSUserDefaults at all, and instead he is archiving & retrieving the high score from a "highScore.archive" file store in your app's Document directory.

Any solution you come up with should forget about user defaults and instead rely on your HighScore object that you instantiate (which loads and saves into that highScore.archive file).

Upvotes: 1

Related Questions