Really simple one, I am sure this is a bug in swift. I am trying to check if 2 NSDates are the same. This statement never passes even thought the printout is below
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
Solution As pointed out by one the awesome people who replied, the dates are probably the same up until sub second level. The odd part is, is these values are coming from HealthKit
What works for me in iOS8 is:
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()))
}
The simplest way to check if dates are equal is to use NSDate.isEqualToDate() method.
or
NSCalendar.currentCalendar().compareDate(date1, toDate: date2, toUnitGranularity: NSCalendarUnit.CalendarUnitSecond)
If you are using quite a bit of date arithmetic check this library.