【发布时间】:2014-11-25 00:53:05
【问题描述】:
NSDateComponents week 自 iOS8 以来已弃用。 Apple 建议使用 weekOfMonth 或 weekOfYear 代替,具体取决于上下文,但它们的工作方式不同。例如:
components1 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
fromDate:[self date7DaysAgo]
toDate:[NSDate date]
options:0];
components1 返回月 = 0,周 = 1,日 = 0,weekOfMonth 和 weekOfYear 都等于 2147483647(整数最大值)。
components2 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
fromDate:[self date30DaysAgo]
toDate:[NSDate date]
options:0];
components2 返回月份 = 1(我知道这取决于,但这只是一个示例),周 = 0,日 = 0,weekOfMonth 和 weekOfYear 都等于 2147483647(整数最大值)。
Week 返回某个时间段内的周数(如果是一周或多个),它与一个月或一年中的一周数完全不同。为什么苹果会建议使用它呢?
【问题讨论】:
-
date7DaysAgo 和 date30DaysAgo 方法执行了什么?
-
返回 7 或 30 天前的日期。例如:'[[NSDate date] dateByAddingTimeInterval:-60 * 60 * 24 * 7] (我知道由于夏令时或其他原因,它可能会有所不同,但同样,这只是一个例子)。
-
如果您真的选择查看文档,NSWeekCalendarUnit 是“在 iOS 8.0 中已弃用”。 (然而,奇怪的是,所有其他单位也是如此。)
-
我本来预计会出现“iOS8 中已弃用”的警告 :) 非常感谢!
标签: ios iphone ios8 nsdatecomponents