I´m having some fun with swift and I´m trying to make a simple game. I got a few variables that changes during the game. What´s the best practice for saving those variables if <
If its just a few variables you want to store and manage you could use NSUserDefaults
.
// Create defaults
let defaults = NSUserDefaults.standardUserDefaults()
// Set an int for highScoreKey in our defaults
defaults.setInteger(10, forKey: "highScoreKey")
// Sync/Save the defaults we've changed
defaults.synchronize()
// Then to retrieve our highScoreKey default we've saved
// We create an int to store the value that is in our default
var highScoreInt = defaults.integerForKey("highScoreKey")
println(highScoreInt)
// Prints 10
I'd set and save these values as soon as I have the values I need rather than in applicationDidEnterBackground
though.
NSUserDefaults Class Reference