-3

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)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
tanman
  • 175
  • 10

4 Answers4

2

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")
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
AdminXVII
  • 1,319
  • 11
  • 22
1

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


        }
nachshon f
  • 3,540
  • 7
  • 35
  • 67
1

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 :)

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
-1

Use NSUserDefaults to store the high score.

Nazik
  • 8,696
  • 27
  • 77
  • 123
rounak
  • 9,217
  • 3
  • 42
  • 59