我的代码的目标是每5秒调用一次函数,它成功完成。问题是,当我每次都声明我的秒变量时,即使开始日期不应该改变,它也不会改变。
func timedUpdate()
{
let initialBank = 2000
let dateOfInstallation = UserDefaults.standard.object(forKey: "installDate")
let seconds = date.timeIntervalSince(dateOfInstallation as! Date) / 8.64
let bestStreak = (UserDefaults.standard.integer(forKey: "bestStreak"))
let days = seconds / 86400
print(seconds/8.64)
print(UserDefaults.standard.object(forKey: "bestStreak")!)
if (Int(days) > bestStreak)
{
UserDefaults.standard.set(days, forKey: "bestStreak")
}
bestStreakLabel.text = "\(Int(bestStreak)) Days"
currentStreakLabel.text = "\(Int(days)) Days"
currentStreakBar.progress = CGFloat(Float(days)/Float(bestStreak))
bestStreakBar.progress = 1
getTodaysSteps { [weak self] steps in
self?.bankTotalLabel.text = "\(Int(steps) - Int(seconds/8.64) + Int(initialBank))"
self?.circleLabel.progress = Double(((Double(steps) - Double(seconds) + Double(initialBank))) / 10000)
if (Int(steps) - Int(seconds) + Int(initialBank) < 0)
{
// Will Eventually need a popup screen here to say you lost and streak is reset to 0
self?.resetApp()
}
}
}
当我打印秒的值时,它始终是相同的小数,非常接近0.请提前感谢您的帮助!