我正在尝试将保存到 UserDefaults 的值与新整数进行比较,但我无法弄清楚。
func setScore() {
let defaults = UserDefaults.standard
let newScore = score
if defaults.object(forKey: "HighScore") != nil {
defaults.set(newScore, forKey: "HighScore")
let highScore = defaults.object(forKey: "HighScore") as! Int
print(highScore)
} else if defaults.object(forKey: "HighScore") < Int(newScore) {
defaults.set(newScore, forKey: "HighScore")
let highScore = defaults.object(forKey: "HighScore") as! Int
print(highScore)
} else {
}
}
如何将值从更改defaults.object(forKey: "HighScore")
为整数,以便我可以比较它们?