1

I am having some problems while attempting to compare my score and highscore to see which one is bigger and then save it. I think the problem lies in the comparison and saving of the highscore and score. Help would be greatly appreciated!

//Comparison between score and highscore
if score > highscore {
let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    highscore = defaults.valueForKey("highscore")?.integerValue ?? 0
    defaults.setInteger(score, forKey: "highscore")
    defaults.synchronize()
    highscoreString = String(highscore)
    highscoreLabel.text = highscoreString  }

Full Page Code - https://github.com/SRL311/Highscore/blob/master/Highscore (Sorry about the confusing capitals and lowercases there)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
hAPPy
  • 69
  • 5
  • It is not being saved when I reopen the page. Also, if you look at the link on the bottom I have multiple highscores on that page and it seems to be passing the scores down when it shouldn't be. – hAPPy Jan 06 '15 at 00:51
  • 1
    Use the debugger and step through the code and look at the values of your variables. – rmaddy Jan 06 '15 at 00:53
  • I didn't understand what is the issue you are talking about. But from your code `String(highscore)` it's not correct, it should be `String(score)` – Midhun MP Jan 06 '15 at 01:02
  • 1
    1. You are not assigning `score` to `highscore`. 2. There is no reason to get the old `highscore` from `NSUserDefaults `. 3. There is no need to `synchronize`. – zaph Jan 06 '15 at 01:04

3 Answers3

5

You can also create a new NSUserDefault property with a getter and a setter to automatically check if the score you are trying to save it is higher than the actual high score:

update: Xcode 8.2.1 • Swift 3.0.2

extension UserDefaults {
    static let highScoreIntegerKey = "highScoreInteger"
    static let highScoreDoubleKey = "highScoreDouble"
    var highScore: Int {
        get {
            print("High Score:", integer(forKey: UserDefaults.highScoreIntegerKey))
            return integer(forKey: UserDefaults.highScoreIntegerKey)
        }
        set {
            guard newValue > highScore
            else {
                print("\(newValue) ≤ \(highScore) Try again")
                return
            }
            set(newValue, forKey: UserDefaults.highScoreIntegerKey)
            print("New High Score:", highScore)
        }
    }
    func resetHighScore() {
        removeObject(forKey: UserDefaults.highScoreIntegerKey)
        print("removed object for highScoreIntegerKey")
    }
    var highScoreDouble: Double {
        get {
            return double(forKey: UserDefaults.highScoreDoubleKey)
        }
        set {
            guard newValue > highScoreDouble
            else {
                print("\(newValue) ≤ \(highScoreDouble) Try again")
                print("Try again")
                return
            }
            set(newValue, forKey: UserDefaults.highScoreDoubleKey)
            print("New High Score:", highScoreDouble)
        }
    }
    func resetHighScoreDouble() {
        removeObject(forKey: UserDefaults.highScoreDoubleKey)
        print("removed object for highScoreDoubleKey")
    }
}

testing in a Project (doesn't work in playground since Swift 2):

UserDefaults().resetHighScore()
UserDefaults().highScore       // High Score = 0
UserDefaults().highScore = 100 // New High Score = 100
UserDefaults().highScore = 50  // 50 < 100 Try again
UserDefaults().highScore       // 100
UserDefaults().highScore = 150 // New High Score = 150
UserDefaults().highScore       // 150 

Xcode 7.2 • Swift 2.1.1

extension NSUserDefaults {
    var highScore: Int {
        get {
            print("High Score = " + integerForKey("highScore").description)
            return integerForKey("highScore")
        }
        set {
            guard newValue > highScore else { print("\(newValue) ≤ \(highScore) Try again")
                return
            }
            setInteger(newValue, forKey: "highScore")
            print("New High Score = \(highScore)")
        }
    }
    func resetHighScore() {
        removeObjectForKey("highScore")
        print("removed object for key highScore")
    }
    var highScoreDouble: Double {
        get {
             return doubleForKey("highScoreDouble")
        }
        set {
            guard newValue > highScoreDouble else { print("Try again")
                return
            }
            setDouble(newValue, forKey: "highScoreDouble")
            print("New High Score = \(highScoreDouble)")
        }
    }
    func resetHighScoreDouble() {
        removeObjectForKey("highScoreDouble")
        print("removed object for key highScoreDouble")
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
1

Read your code carefully and you should be able to find the errors.

  1. You are not assigning score to highscore.
  2. There is no reason to get the old highscore from NSUserDefaults.
  3. There is no need to synchronize.
zaph
  • 111,848
  • 21
  • 189
  • 228
0

Swift 2: You have set an integer value in userDefaults so, you may try retrieve it using, defaults.integerForKey(score, forKey: "highscore")

Aashish
  • 2,532
  • 2
  • 23
  • 28