panderson14
panderson14

Reputation: 37

Swift 3.0 save a score with SpriteKit

I am trying to save a "highScore" integer that persists after the app is closed using SpriteKit.

That's it. Just one single integer on one single screen that I will eventually set with my game whether or not the new score is higher than the saved score.

I understand that there are a lot of options already on stackoverflow for this, however they are all for versions of Swift less than 3.0. They use a bunch of methods and built-in functionality that I don't understand as a beginner. After trying to convert all of these options to Swift 3.0 and failing, I am hoping someone can show me some sample code how to do this.

I will be happy to answer any further questions.

Upvotes: 1

Views: 374

Answers (1)

alexburtnik
alexburtnik

Reputation: 7741

You can use UserDefaults class for that purpose.

If you have any sort of GameManager singleton, you can define a computed variable which saves and reads from UserDefaults:

var highScore: Int {
    get {
        return UserDefaults.standard.integer(forKey: "highScore")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "highScore")
    }
}

Upvotes: 3

Related Questions