0

Swift Playground で以下のコードを実行しました。どちらも開始点と同じ startDt を持っているため、「最初の秒」のカウントは 5 で、「秒の秒」のカウントは 10 であると予想しました。しかし、どちらも出力で 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)")
4

1 に答える 1