我正在使用UserDefaults保存值,以确定何时在应用程序中展示插页式广告。
以下是用于递增数字的代码:
5.2+-0.0010
我声明用户默认如下if((defaults.integer(forKey: PRESENT_INTERSTITIAL_NUMBER) > defaults.integer(forKey: CURRENT_INTERSTITIAL_NUMBER))){
print("1111")
let oldNumber = defaults.integer(forKey: CURRENT_INTERSTITIAL_NUMBER)
let newNumber = oldNumber + 1
defaults.set(newNumber, forKey: CURRENT_INTERSTITIAL_NUMBER)
print("oldNumber \(oldNumber)")
print("newNumber \(newNumber)")
print("INTER NUMBER \(defaults.integer(forKey: CURRENT_INTERSTITIAL_NUMBER))")
print(defaults.integer(forKey: PRESENT_INTERSTITIAL_NUMBER))
}
我的问题是它并不总是正确地增加值。我已经玩了大约一个小时来尝试解决这个问题。
情况1:
let defaults = UserDefaults.standard
...运行递增代码...
CURRENT_INTERSTITIAL_NUMBER = 0
PRESENT_INTERSTITIAL_NUMBER = 4
...运行递增代码...
CURRENT_INTERSTITIAL_NUMBER = 1
PRESENT_INTERSTITIAL_NUMBER = 4
...运行递增代码...
CURRENT_INTERSTITIAL_NUMBER = 1
PRESENT_INTERSTITIAL_NUMBER = 4
...运行递增代码...
CURRENT_INTERSTITIAL_NUMBER = 1
PRESENT_INTERSTITIAL_NUMBER = 4
当该值不能正确结转时,这似乎是随机的,但我不知道为什么这样做。
谢谢!