Z S*_*Z S 7 cocoa cocoa-touch objective-c nsdate nsdatecomponents
在我的应用程序中,我必须设置每周提醒,以便在一周后的同一天/时间发出警报.我一直在使用NSDateComponenents.week = 1并使用NSCalendar的dateByAddingComponents:toDate:options:API 将其添加到NSDate .似乎week现在在iOS7中被弃用,警告告诉我"使用weekOfMonth或weekOfYear,具体取决于你的意思".但我找不到关于其中任何一个含义的文档.
谁能解释两者之间的区别?在基本测试中,两者在添加到NSDate时返回相同的值,但我确信两者之间存在一些有意义的差异.
两个节目之间的差异显示了相对于月份或年份的周.
2月的第二周(weekOfMonth)将是一年中的第七周(weekOfYear),例如,取决于年份.
至于使用日期组件将日期添加到日期,使用哪个似乎并不重要,但在Internet上,weekOfYear似乎是首选.
我在Swift游乐场做了一些测试,他们总是在将日期添加到日期时计算相同的值.

我一直发现,iOS似乎很难处理几周。
weekOfMonth 将是1,2,3,4等等...
weekOfYear 将是1到52(我认为)
他们似乎不是持续时间的度量。就像“ 4月3日”不是持续时间的度量标准,而“ 1天”是持续时间的度量标准。
最好增加一个星期的方法是...
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:7];
NSDate *date = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
Run Code Online (Sandbox Code Playgroud)
编辑
经过一点测试,我是对的。
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear fromDate:date];
Printing description of date:
2014-08-20 08:15:13 +0000
Printing description of comps:
<NSDateComponents: 0xb7753c0>
Week of Year: 34
Week of Month: 4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5587 次 |
| 最近记录: |