-1

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.

Community
  • 1
  • 1
SDGary
  • 434
  • 3
  • 17

1 Answers1

2

The problem is that you have written the name of the components: parameter as component:. Those are not the same thing, as you should know if you are an "experienced C programmer" - just as two variables called thing and things are not the same.

If you correct that error, then your change to modernize the names of the components will compile just fine, without any warnings:

+ (BOOL) isToday:(NSDate *)aDate {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:aDate];
    NSDate *otherDate = [cal dateFromComponents:components];
    BOOL isToday = [today isEqualToDate:otherDate];
    return isToday;
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • However, I don't guarantee that that will run in iOS 7, because I don't know when terms like `NSCalendarUnitEra` replaced terms like `NSEraCalendarUnit`. If it won't, then you will just have to live with the deprecation warnings. – matt Apr 08 '15 at 00:57
  • Changes in enum element names are purely a compile time thing. It can't cause a problem when running on iOS 7, because the values are identical. – Ken Thomases Apr 08 '15 at 01:38
  • @KenThomases Right, I was thinking there could be a problem, but once it compiles for iOS 8 it's all just numbers as far as iOS 7 is concerned. Thanks for pointing that out! – matt Apr 08 '15 at 02:02
  • 1
    @Matt Thank you! I am visually impaired, I missed the "component" vs. "components" change. I need to pay more attention to where Xcode code completion stops, to identify these similarly named methods. – SDGary Apr 09 '15 at 01:11
  • @SDGary It is a huge problem no matter how well you can see! But unfortunately it makes a major difference. :( – matt Apr 09 '15 at 02:30