var window: UIWindow?
func application(_ application: UIApplication, options: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
UserDefaults.standard.register(defaults: [
bestScoreKey: 1_000_000.0
])
return true
}
func application2(_ application: UIApplication, options: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
UserDefaults.standard.register(defaults: [
coinsKey: 0
])
return true
}
public func application3(_ application: UIApplication, options: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
UserDefaults.standard.register(defaults: [
standardHSKey: 0
])
return true
}
以上是AppDelegate.Swift中的三个函数。出于某种原因,前两个功能起作用但不起作用。以下是ViewControllers。在这个ViewController中,数据保存。
var fastestScore: Double = UserDefaults.standard.double(forKey: bestScoreKey) {
didSet {
UserDefaults.standard.set(fastestScore, forKey: bestScoreKey)
}
}
var coinsTotal: Int = Int(UserDefaults.standard.double(forKey: coinsKey)) {
didSet {
UserDefaults.standard.set(coinsTotal, forKey: coinsKey)
}
}
但是,它不会保存此viewController中的数据
var highScore: Int = Int(UserDefaults.standard.double(forKey: standardHSKey){
didSet {
UserDefaults.standard.set(highScore, forKey: standardHSKey)
}
}
我不知道问题所在。当我用一个整数替换highScore它可以工作,但它不会将数据保存到Userdefaults。有没有人有任何建议?