AppDelegate.Swift中的用户默认值不工作
上面是AppDelegate.Swift中的三个函数。出于某种原因,最上面的两个函数可以工作,但最下面的一个却不行。下面是ViewController。在此ViewController中,数据将保存AppDelegate.Swift中的用户默认值不工作,swift,xcode,Swift,Xcode,上面是AppDelegate.Swift中的三个函数。出于某种原因,最上面的两个函数可以工作,但最下面的一个却不行。下面是ViewController。在此ViewController中,数据将保存 var window: UIWindow? func application(_ application: UIApplication, options: [UIApplicationLaunchOptionsKey : Any]?) -> Bool { UserDefaults.
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
}
但是,它不会将数据保存在此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)
}
}
我不知道是什么问题。当我用整数替换highScore时,它可以工作,但不会将数据保存为UserDefault。有人有什么建议吗?可能重复的.synchronize()没有called@g在上更改任何内容后,我将把synchronize()放在何处UserDefaults@ReidBrown
我应该在哪里放置synchronize()
无处放置。自从iOS8之后,就不再需要这个了。人们告诉其他人添加synchronize()
并没有更新他们对此的知识,这只是一个现在已经无用的习惯。文档中对此进行了解释(并通过测试进行了验证)。
var highScore: Int = Int(UserDefaults.standard.double(forKey: standardHSKey){
didSet {
UserDefaults.standard.set(highScore, forKey: standardHSKey)
}
}