【问题标题】:NSDateComponents week is working differently on iOS8NSDateComponents 周在 iOS8 上的工作方式有所不同
【发布时间】:2014-11-25 00:53:05
【问题描述】:

NSDateComponents week 自 iOS8 以来已弃用。 Apple 建议使用 weekOfMonthweekOfYear 代替,具体取决于上下文,但它们的工作方式不同。例如:

components1 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
                          fromDate:[self date7DaysAgo]
                            toDate:[NSDate date]
                           options:0];

components1 返回月 = 0,周 = 1,日 = 0,weekOfMonthweekOfYear 都等于 2147483647(整数最大值)。

components2 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
                          fromDate:[self date30DaysAgo]
                            toDate:[NSDate date]
                           options:0];

components2 返回月份 = 1(我知道这取决于,但这只是一个示例),周 = 0,日 = 0,weekOfMonthweekOfYear 都等于 2147483647(整数最大值)。

Week 返回某个时间段内的周数(如果是一周或多个),它与一个月或一年中的一周数完全不同。为什么苹果会建议使用它呢?

【问题讨论】:

  • date7DaysAgo 和 date30DaysAgo 方法执行了什么?
  • 返回 7 或 30 天前的日期。例如:'[[NSDate date] dateByAddingTimeInterval:-60 * 60 * 24 * 7] (我知道由于夏令时或其他原因,它可能会有所不同,但同样,这只是一个例子)。
  • 如果您真的选择查看文档,NSWeekCalendarUnit 是“在 iOS 8.0 中已弃用”。 (然而,奇怪的是,所有其他单位也是如此。)
  • 我本来预计会出现“iOS8 中已弃用”的警告 :) 非常感谢!

标签: ios iphone ios8 nsdatecomponents


【解决方案1】:

使用NSWeekOfMonthCalendarUnitNSWeekOfYearCalendarUnit 代替NSWeekCalendarUnit

【讨论】:

  • 我的错。我认为它在 iOS8 上的工作方式有所不同,但 weekOfYear 会在这里完成工作。
  • 那些在 8 中已被弃用。(祝你在 Apple 的文档中找到替代品。)
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2014-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多