Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - what is the difference between NSWeekCalendarUnit and NSWeekdayCalendarUnit?

Tags:

iphone

I am trying to set a repeatingInterval of a UILocalNotification using these values but, as always, Apple docs are vague as hell.

Any clues?

thanks.

like image 270
Duck Avatar asked Sep 17 '11 11:09

Duck


2 Answers

Maybe look at this blog I just found about the subject?

AFAIK, NSCalendarUnits are primarely used to split a date or timeInterval into date components (NSDateComponents), to extract the weekday of a date, the year of the date, the hour component of a time, and so on.

In this context:

  • the NSWeekCalendarUnit of a date correspond to the week index in the year (from 1st to 52nd week - or 53rd for years with 53 weeks)
  • NSWeekdayCalendarUnit corresponds to the day in the week (from Mon to Sun)
  • NSDayCalendarUnit corresponds to the day in the year (from 1 to 365)

When using the NSCalendarUnit type with repeatingInterval, the UILocalNotification will be triggered when the corresponding unit changes:

  • NSWeekCalendarUnit will trigger the notification every week (every 7 days)
  • NSWeekdayCalendarUnit will trigger the notification "every weekday", which corresponds to the same thing here as NSDayCalendarUnit which corresponds to "every day" in the context of a repeatingInterval.
like image 182
AliSoftware Avatar answered Oct 05 '22 15:10

AliSoftware


Cocoa and Objective-C make it easy to knock up quick test programs to see results. If you aren't sure of the documentation you can always check for yourself. I've got a project that builds a Foundation Command Line tool, and I just type these snippets into the main.m and log 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 got

Week: 37
Weekday: 7

It's quick to see that WeekCalendar unit gives this as the 37th week of the year and WeekDay says this (it's Saturday today) is the 7th day of the week ('cos I know that the Gregorian calendar counts Sunday as day 1).

like image 22
Abizern Avatar answered Oct 05 '22 16:10

Abizern