I'm trying to get the number of the week of the year out of an NSDate
with this code:
int which = NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekOfYearCalendarUnit | NSQuarterCalendarUnit;
NSDate* date= [NSDate date];
NSDateComponents* comps= [[NSCalendar currentCalendar] components:which fromDate:date];
today.weekNumber= comps.weekOfYear;
However, for some reason it is crashing and stating:
reason: -[NSDateComponents weekOfYear]: unrecognized selector sent to instance
The other components work just fine when I uncomment the weeknumber part:
int which = NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekOfYearCalendarUnit | NSQuarterCalendarUnit;
NSDate* date= [NSDate date];
NSDateComponents* comps= [[NSCalendar currentCalendar] components:which fromDate:date];
// today.weekNumber= comps.weekOfYear;
today.month= comps.month;
today.year= comps.year;
today.quarter= comps.quarter;
today.hour= comps.hour;
today.minute= comps.minute;
today.second= comps.second;
According to the docs, NSDateComponents
should have a property called weekOfYear
, shouldn't it?
I'm confused, really.
-weekOfYear
method is available in NSDateComponents
starting iOS 5.0 and so if you're trying to use that method on earlier OS versions that will result in the crash.