What is the difference between NSWeekCalendarUnit and NSWeekdayCalendarUnit?

I am trying to set a repeating UILocalNotification interval using these values, but, as always, Apple docs are not clear as hell.

Any clues?

thanks.

+4
source share
3 answers

Perhaps check out this blog I just found about the subject?

AFAIK, NSCalendarUnits are primarily used to split the date or time of Interval into date components ( NSDateComponents ), to retrieve the day of the week, date of the year, hourly time component, etc.

In this context:

  • NSWeekCalendarUnit dates correspond to the weekly index of the year (from the 1st to the 52nd week or the 53rd during the years with 53 weeks).
  • NSWeekdayCalendarUnit corresponds to the day of the week (from Mon to Sun)
  • NSDayCalendarUnit corresponds to a day of the year (1 to 365).

When using the NSCalendarUnit type with repeatingInterval , UILocalNotification will be activated when the corresponding block changes:

  • NSWeekCalendarUnit will trigger a notification every week (every 7 days)
  • NSWeekdayCalendarUnit will trigger a “every weekday” notification, which corresponds to the same as NSDayCalendarUnit , which corresponds to “every day” in the context of a repeating NSDayCalendarUnit .
+4
source

Cocoa and Objective-C make it easy to run quick test programs to view results. If you are not sure about the documentation, you can always check it yourself. I have a project that creates the Foundation command-line tool, and I just type these fragments in main.m and register them just to see what the API returns.

For example, I just ran this:

 unsigned flags = NSWeekCalendarUnit; NSDateComponents *components = [[NSCalendar currentCalendar] components:flags fromDate:[NSDate date]]; NSLog(@"Week: %ld", [components week]); flags = NSWeekdayCalendarUnit; components = [[NSCalendar currentCalendar] components:flags fromDate:[NSDate date]]; NSLog(@"WeekDay: %ld", [components weekday]); 

And received

 Week: 37 Weekday: 7 

It’s quick to see that WeekCalendar gives it as the 37th week of the year, and WeekDay says that (today is Saturday) is the seventh day of the week ("because I know that the Gregorian calendar counts as Sunday as day 1).

+2
source

NSWeekdayCalendarUnit means every Sunday ur notification will be sent.

NSWeekCalendarUnit means your notification will be sent every seventh day

0
source

All Articles