使用NSUserDefaults在Swift中保存和加载高分

时间:2015-01-06 00:45:38

标签: ios swift nsuserdefaults

我在尝试比较我的得分和高分时遇到一些问题,看看哪一个更大然后保存。我认为问题在于比较和保存高分和得分。非常感谢帮助!

//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  }

整页代码 - https://github.com/SRL311/Highscore/blob/master/Highscore (抱歉那里有令人困惑的大写字母和小写字母)

3 个答案:

答案 0 :(得分:5)

您还可以使用getter和setter创建一个新的NSUserDefault属性,以自动检查您尝试保存它的分数是否高于实际的高分:

更新: 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")
    }
}

在项目中测试(自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")
    }
}

答案 1 :(得分:1)

仔细阅读您的代码,您应该能够找到错误。

  1. 您没有将score分配给highscore
  2. 没有理由从highscore获取旧的NSUserDefaults
  3. 无需synchronize

答案 2 :(得分:0)

Swift 2:您已在 userDefaults 中设置了一个整数值,因此,您可以尝试使用defaults.integerForKey(score, forKey: "highscore")

检索它