Search code examples
iosnscalendar

[NSCalendar calendarWithIdentifier:]: unrecognized selector sent to class 0x108db9208`


I have a method which adds years to a date. While it works on my iPhone 5c iOS8, it fails in the simulator 5s iOS7.1

Here the method:

+ (NSDate *)addYears:(int)years toDate:(NSDate *)datum{
    NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSGregorianCalendar];
    NSDateComponents *components= [[NSDateComponents alloc] init];
    [components setYear:years];
    return [gregorian dateByAddingComponents:components toDate:datum options:0];
}

The exception is thrown in NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSGregorianCalendar];

+[NSCalendar calendarWithIdentifier:]: unrecognized selector sent to class 0x108db9208

Any ideas?


Solution

  • That's a method of EKEventStore, not NSCalendar.

    You want:

    NSCalendar *gregarian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];