How would I store the high score. When I exit out of the app the high score goes back to 0. This is the code which will determine the high score.
if(scoring.text > best.text){
best.text = String(score)
}
How would I store the high score. When I exit out of the app the high score goes back to 0. This is the code which will determine the high score.
if(scoring.text > best.text){
best.text = String(score)
}
The option I recommend you to use is NSUserDefaults
. You can store an int like that:
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "HighScore")
and retrieve it like that:
NSUserDefaults.standardUserDefaults().integerForKey("HighScore")
Use NSUserDefaults. See Code Below...
Save The Score
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.scorelabel.text, forKey: "Score")
defaults.synchronize()
Load The Score Back
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if let scoreIsNotNill = defaults.objectForKey("Score") as? String {
self.scorelabel.text = defaults.objectForKey("Score") as String
}
Here is how to save data:
let defaults = NSUserDefaults.standardUserDefaults()
defaults?.setObject(score, forKey: "HighScore")
defaults?.synchronize()
You can read it the following way:
let x = defaults.valueForKey("HighScore") as! Int //or String, depending on what you need
Hope that helps :)