Reputation: 449
I am just learning to program and doing some SWIFT tutorials. I have done as much research as I could but seem to be missing some critical aspect. I have a simple game and when a player dies I want to check their score against the previously saved high. If it's higher it's to save the new score.
On a title screen the highest score is displayed.
Here is the current code I am using;
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
NSUserDefaults.standardUserDefaults().synchronize()
}
NSUserDefaults.standardUserDefaults().integerForKey("highscore")
The problem I cannot solve is simply how to print the highscore! I tried using;
hiScoreText.text = "Hi Score : \(highscore)"
but as I guessed that isn't correct. What variable do I use or how do I "read" the highscore that has been saved?
Any advice for a new guy would be greatly appreciated.
Upvotes: 1
Views: 175
Reputation: 27335
I guess you forgot to save retrieved highs score to the variable:
let highscore = NSUserDefaults.standardUserDefaults().integerForKey("highscore")
Upvotes: 2
Reputation:
You have to store the result of NSUserDefaults.standardUserDefaults().integerForKey("highscore")
inside a variable that you print.
Upvotes: 2