从固定开始时间计算不同时间点的经过时间

Calculating elapsed time at different points of time from a fixed start time

我在 Swift 游乐场中执行了以下代码。我预计 "First Seconds" 计数为 5,"Second Seconds" 计数为 10,因为两者都具有与起点相同的 startDt。但是输出结果都是5。我在这里遗漏了什么吗?

let startDt = NSDate()
sleep(5)
let endDt = NSDate()
let calendar = NSCalendar.currentCalendar()
let datecomponenets = calendar.components([NSCalendarUnit.Second] , fromDate: startDt, toDate: endDt, options: [] )
let seconds = datecomponenets.second
print("First Seconds: \(seconds)")

sleep(5)
let endDt1 = NSDate()
let calendar1 = NSCalendar.currentCalendar()
let datecomponenets1 = calendar.components([NSCalendarUnit.Second] , fromDate: startDt, toDate: endDt1, options: [] )
let seconds1 = datecomponenets.second
print("Second Seconds: \(seconds1)")

你打错了。

let seconds1 = datecomponenets.second

应该是

let seconds1 = datecomponenets1.second

然后它将按预期工作。