Saving And Loading Up A Highscore In Swift Using N

2019-01-12 08:18发布

问题:

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)

回答1:

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")
    }
}


回答2:

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.


回答3:

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