问题
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.
回答1:
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")
}
}
来源:https://stackoverflow.com/questions/40330303/swift-3-0-save-a-score-with-spritekit