iosobjective-cnscalendarcal

NSCalendar [_NSCopyOnWriteCalendarWrapper component:fromDate:]: error


In a tableviewcontroller I have this code to get an array of days from 0 to 6 where it is order with the current dayNumber first.

self.dayOrder=[NSMutableArray new];

NSCalendar *cal=[NSCalendar currentCalendar];

NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,

for (int i=0;i<7;i++) {
    [self.dayOrder addObject:[NSNumber numberWithInteger:dayNumber]];
    dayNumber=(dayNumber+1)%7;
}

When I move to this view controller I get this error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSCopyOnWriteCalendarWrapper component:fromDate:]: unrecognized selector sent to instance 0x15e4b850'

And when I add an ALL Exceptions break point the error is occuring at this line

 NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,

It seems to be working on my iPhone 6 but which is on iOS 8 but not iPone 4,5c which are on iOS7.

Not sure why this is happening, any help of why it is would be greatly appreciated.

Thanks for the help!!!


Solution

  • The component:fromDate: method was added to NSCalendar in iOS 8.0.

    If you need to support older iOS versions, then use the older components:fromDate: method and extract the value you need from the resulting NSDateComponents.

    NSDateComponents *components = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]];
    NSInteger weekday = [components weekday] - 1;