真的很简单,我确信这是一个迅速的错误。我正在尝试检查2个NSDate是否相同。即使认为打印输出在下面,该语句也不会通过
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.isEqualToDate()方法。
或者
NSCalendar.currentCalendar().compareDate(date1, toDate: date2, toUnitGranularity: NSCalendarUnit.CalendarUnitSecond)
如果您使用大量的日期算法,请检查此库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句