gpt4 book ai didi

swift - NSTimer 倒计时产生两次 0

转载 作者:可可西里 更新时间:2023-11-01 02:15:15 26 4
gpt4 key购买 nike

我有一些代码可以获取今天和另一个时间字符串之间的时间差。

使用 NSTimer.scheduledTimerWithTimeInterval 每秒刷新显示。

除计时器达到 0 外,一切正常。0 显示两次。

例子:

3,2,1,0,0,-1,-2 

enter image description here

下面是我在 Playground 上完成的代码:

   //: Playground - Time Countdown
import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()

class MyClass {

var totalSecondsText = ""

init(classSecondsText: String){
totalSecondsText = classSecondsText
}

@objc func ontimer(timer:NSTimer!){

let total = determineTimeDifference(totalSecondsText)

let (hour,min,sec) = secondsConverter(total)

if hour >= 1 {
if min >= 1 {
NSLog(String(hour)+"h "+String(min)+"m")
}else {
NSLog(String(hour)+"h")
}
}else{
if min >= 1{
NSLog(String(min)+"m "+String(sec)+"s")
} else if min > -1 {
NSLog(String(sec)+"s")
} else if min <= -1 {
NSLog(String(min)+"m")
}
}
}

func determineTimeDifference(timeInputed: String) -> Int {

let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let finalDate = dateFormat.dateFromString(raceTime)
let differenceTimeInSeconds = (finalDate?.timeIntervalSinceNow)!

return differenceInSeconds

}

func secondsConverter (seconds : Int) -> (Int , Int, Int){
return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
}

}

// Add +1 minute on time to test.
var dateString = "2016-08-31T6:49:00Z"

NSTimer.scheduledTimerWithTimeInterval(1, target: MyClass(classSecondsText: dateString), selector: #selector(MyClass.ontimer(_:)), userInfo: nil, repeats: true)

知道哪里出了问题吗?

更新:

正如所 promise 的,这是针对此问题的错误修复:

    func determineTimeDifference(timeInputed: String) -> Int {

let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let finalDate = dateFormat.dateFromString(raceTime)?.timeIntervalSince1970
let dateNow = NSDate().timeIntervalSince1970
let differenceTimeInSeconds = Int(finalDate) - Int(dateNow)

return differenceInSeconds
}

最佳答案

您的代码提供得很好,并且条件设置正确。看起来 Data.components 没有正确处理计数差异。这可能是由秒组件的舍入引起的。

如果在时刻之前计数,当当前时间和最终时间相等时,(达到 0,0),结果会出现错误 -1。 p>

当我用额外的打印更新您的代码时,显示如下:

CURRENT date: 2016-08-31 13:10:57 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 2s (BAD)
CURRENT unix: 1472649057.28685 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -3.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
2s

CURRENT date: 2016-08-31 13:10:58 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 1s (BAD)
CURRENT unix: 1472649058.28734 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -2.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
1s

CURRENT date: 2016-08-31 13:10:59 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 0s (BAD)
CURRENT unix: 1472649059.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: -1.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
0s

CURRENT date: 2016-08-31 13:11:00 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: 0s (OK)
CURRENT unix: 1472649060.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 0.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
0s

CURRENT date: 2016-08-31 13:11:01 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: -1s (OK)
CURRENT unix: 1472649061.28731 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 1.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
-1s

CURRENT date: 2016-08-31 13:11:02 +0000 - FINAL date: 2016-08-31 13:11:00 +0000
-->> diff by DateComponents: -2s (OK)
CURRENT unix: 1472649062.28729 - FINAL unix: 1472649060.0
--->>> Diff in UNIX: 2.0s (counted as Rounded CURRENT UNIX - FINAL UNIX)
-2s

因此,如果使用四舍五入的 unix 时间之间的差异进行计数,结果是正确的。

结论:误差有时出现在等式之前,有时出现在等式之后。这取决于该脚本启动的时间。如果按数据组件计数以错误开始,则在 0、0 之后进行清理。如果以正确结果开始计数,则结果开始带来错误结果。这就是零出现两次的原因 - 在结果改变其精确状态的那一刻。

关于swift - NSTimer 倒计时产生两次 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39242509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com