I am an experienced Unix C programmer, relatively new to Objective C and OS X. I want to determine if an NSDate is today. My method is copied from a good solution here:
+ (BOOL) isToday:(NSDate *)aDate {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:aDate];
NSDate *otherDate = [cal dateFromComponents:components];
BOOL isToday = [today isEqualToDate:otherDate];
return isToday;
}
The code compiles, but generated deprecation warnings for each NSInteger NSxxxCalendarUnit. A quick search shows the updated constants of the form NSCalendarUnixxxx.
How do I use the new constants? I tried replacing the line:
components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:aDate];
With
components = [cal component:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:aDate];
But I get conversion warnings. Do I need to explicitly cast this OR'd constant? The new constants are of type NSUInteger, NSCalendarUnit. The "components:" argument seems to be expecting NSCalendarUnit, so I'm not sure what I am missing...
And, yes, I know that iOS8 and OS X 10.10 include new calendar functions (described here), such as isDateInToday:, but I am looking for an iOS 7 compatible solution.