-2

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
  • 307,833
  • 40
  • 508
  • 550
tanman
  • 177
  • 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,319
  • 6
  • 42
  • 50
AdminXVII
  • 1,264
  • 14
  • 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,260
  • 6
  • 32
  • 61
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
  • 24,641
  • 28
  • 109
  • 162
-1

Use NSUserDefaults to store the high score.

Nazik
  • 8,607
  • 26
  • 75
  • 122
rounak
  • 8,989
  • 3
  • 39
  • 58