NSDateComponents Week works differently on iOS8

NSDateComponents week deprecated with iOS8. Apple suggests using weekOfMonth or weekOfYear instead, depending on the context, but they work differently. For instance:

 components1 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit fromDate:[self date7DaysAgo] toDate:[NSDate date] options:0]; 

components1 returns month = 0, week = 1, day = 0, weekOfMonth and weekOfYear are 2147483647 (integer maximum value).

 components2 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit fromDate:[self date30DaysAgo] toDate:[NSDate date] options:0]; 

components2 returns month = 1 (I know it depends, but this is just an example), week = 0, day = 0, weekOfMonth and weekOfYear are 2147483647 (integer maximum value).

week returns the number of weeks over a period (if it was a week or more), and this is completely different from the number of weeks in a month or year. Why did Apple suggest using this instead?

+6
source share
1 answer

Use NSWeekOfMonthCalendarUnit or NSWeekOfYearCalendarUnit instead of NSWeekCalendarUnit .

+6
source

Source: https://habr.com/ru/post/976051/


All Articles