0

I have a function that is supposed to keep track of a high score. When I played it the first time on my computer it worked and my high score was 4. Then I installed it on my phone to it test and the high score now is always 4, even if I beat it.

Inside viewDidLoad:

let HighscoreDefault = NSUserDefaults.standardUserDefaults()
if(HighscoreDefault.valueForKey("Highscore") != nil)
{
    circleView1.score = HighscoreDefault.valueForKey("Highscore") as! NSInteger!
    scoreValue.text = Int(circleView1.score.value).description;
}

Inside function to update high score:

if(round>score)
{
    score=round;
    let HighscoreDefault = NSUserDefaults.standardUserDefaults()
    HighscoreDefault.setValue(score, forKey: "Highscore")
    HighscoreDefault.synchronize()
}
tktsubota
  • 9,371
  • 3
  • 32
  • 40
Steve
  • 1,121
  • 1
  • 12
  • 32
  • 2
    You should use the much simpler `setInteger:forKey` and `integerForKey:` methods. You probably need to refresh the high score display somewhere other than `viewDidLoad` as this will only execute once – Paulw11 Feb 19 '16 at 00:15
  • I update it during the game in a different function and it works but it does;t save it – Steve Feb 19 '16 at 00:29
  • Have you set a breakpoint to see if your save code is being called? It looks OK. You don't need the `synchronize` by the way – Paulw11 Feb 19 '16 at 00:30
  • I tried it and i don't think it is being called. how did it save the original 4 though – Steve Feb 19 '16 at 00:33

0 Answers0