In order to avoid this (possibly undefined) behaviour, just test the components you are extracting:
- (BOOL)isInTheSameComponents:(NSUInteger)components withDate:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:components fromDate:self];
NSDateComponents *components2 = [calendar components:components fromDate:date];
if ((components & NSYearCalendarUnit) != 0 && components1.year != components2.year)
return NO;
if ((components & NSMonthCalendarUnit) != 0 && components1.month != components2.month)
return NO;
if ((components & NSDayCalendarUnit) != 0 && components1.day != components2.day)
return NO;
if ((components & NSHourCalendarUnit) != 0 && components1.hour != components2.hour)
return NO;
if ((components & NSMinuteCalendarUnit) != 0 && components1.minute != components2.minute)
return NO;
// and any other components you might use
return YES;
}
I would suggest a better name for this method would be:
- (BOOL)isEqualToComponents:(NSUInteger)components inDate:(NSDate *)date
{
...
}
(I assume this is an NSDate
category)