iPhone - what is the difference between NSWeekCalendarUnit and NSWeekdayCalendarUnit?

后端 未结 3 1174
温柔的废话
温柔的废话 2021-01-03 01:28

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.

相关标签:
3条回答
  • 2021-01-03 01:46

    NSWeekdayCalendarUnit means every sunday ur notification will comming.

    NSWeekCalendarUnit means every 7th day your notification will coming

    0 讨论(0)
  • 2021-01-03 01:48

    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).

    0 讨论(0)
  • 2021-01-03 01:55

    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.
    0 讨论(0)
提交回复
热议问题