我正在尝试使用这些值设置UILocalNotification的repeatingInterval,但是像往常一样,苹果文档非常模糊。 有任何线索吗?谢谢。
NSCalendarUnits
主要用于将日期或时间间隔分解为日期组件 (NSDateComponents
),提取日期的星期几、年份、时间的小时组件等。NSWeekCalendarUnit
对应于一年中的周索引 (从第1周到第52周 - 或者对于有53周的年份是第53周)NSWeekdayCalendarUnit
对应于一周中的某一天 (从周一到周日)NSDayCalendarUnit
对应于一年中的某一天 (从1到365)repeatingInterval
和 NSCalendarUnit 类型时,当相应的单位发生变化时,UILocalNotification 将被触发:
NSWeekCalendarUnit
将每周触发通知 (每7天一次)NSWeekdayCalendarUnit
将触发“每个工作日”的通知,在这里与 NSDayCalendarUnit
相同,它在重复间隔的上下文中对应于“每天”。Cocoa和Objective-C使得很容易编写快速测试程序以查看结果。如果您不确定文档,可以随时自行检查。我有一个构建Foundation命令行工具的项目,我只需在main.m中输入这些片段并记录它们,以查看API返回的内容。
例如,我刚运行了这个:
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]);
并获得
Week: 37
Weekday: 7
很快就能看出,WeekCalendar单元将其作为一年中的第37周给出,而WeekDay则表示这是本周的第7天(因为我知道格里高利历把星期日算作第1天)。
NSWeekdayCalendarUnit 意味着每个星期日您将收到通知。
NSWeekCalendarUnit 意味着每7天您将收到通知。
NSWeekCalendarUnit
= 每周一次。NSDayCalendarUnit
= 每天一次。NSWeekdayCalendarUnit
= 不要在repeatingInterval
上下文中使用它,以避免再次阅读代码时头痛,并将其保留在将日期拆分为NSDateComponents
的上下文中使用。 - AliSoftware