【发布时间】:2015-04-01 13:55:58
【问题描述】:
真的很简单,我确信这是 swift 中的一个错误。我正在尝试检查 2 个 NSDates 是否相同。即使打印输出低于此语句,该语句也不会通过
println("\(statistics.startDate) \(healthObject.date)")
if healthObject.date.isEqualToDate(statistics.startDate)
{
healthObject.flights = Int(quantity.doubleValueForUnit(HKUnit.countUnit()))
println(Int(quantity.doubleValueForUnit(HKUnit.countUnit())))
}
2015-03-30 13:56:50 +0000 2015-03-30 13:56:50 +0000
2015-03-30 13:56:50 +0000 2015-03-31 13:56:50 +0000
2015-03-31 13:56:50 +0000 2015-03-30 13:56:50 +0000
2015-03-31 13:56:50 +0000 2015-03-31 13:56:50 +0000
解决方案 正如一位出色的回答者所指出的那样,在亚秒级之前,日期可能是相同的。奇怪的是,这些值是否来自 HealthKit
在 iOS8 中对我有用的是:
if let dif = calender?.compareDate(statistics.startDate, toDate: healthObject.date, toUnitGranularity: NSCalendarUnit.SecondCalendarUnit)
{
println(Int(quantity.doubleValueForUnit(HKUnit.countUnit())))
println("\(statistics.startDate) \(healthObject.date)")
healthObject.flights = Int(quantity.doubleValueForUnit(HKUnit.countUnit()))
}
【问题讨论】:
-
刚刚尝试了一个非常简单的,似乎可以只使用 NSDate() == NSDate()