ios - NSCalendar [_NSCopyOnWriteCalendarWrapper 组件 :fromDate:]: error

标签 ios objective-c nscalendar cal

在 tableviewcontroller 中,我有这段代码来获取从 0 到 6 的天数数组,其中它以当前天数排在第一位。

self.dayOrder=[NSMutableArray new];

NSCalendar *cal=[NSCalendar currentCalendar];

NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,

for (int i=0;i<7;i++) {
    [self.dayOrder addObject:[NSNumber numberWithInteger:dayNumber]];
    dayNumber=(dayNumber+1)%7;
}

当我移动到这个 View Controller 时,我得到这个错误 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSCopyOnWriteCalendarWrapper component:fromDate:]: unrecognized selector sent to instance 0x15e4b850'

当我添加一个 ALL Exceptions 断点时,错误发生在这一行

 NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,

它似乎可以在我的 iPhone 6 上运行,但它在 iOS 8 上运行,但在 iOS7 上运行的 iPone 4,5c 不可用。

不确定为什么会这样,如果能提供任何帮助,我们将不胜感激。

感谢帮助!!!

最佳答案

component:fromDate: 方法在 iOS 8.0 中添加到 NSCalendar

如果您需要支持旧的 iOS 版本,请使用旧的 components:fromDate: 方法并从生成的 NSDateComponents 中提取您需要的值。

NSDateComponents *components = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]];
NSInteger weekday = [components weekday] - 1;

关于ios - NSCalendar [_NSCopyOnWriteCalendarWrapper 组件 :fromDate:]: error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26784741/

相关文章:

ios - 代码没有进入类方法iOS

objective-c - 像 Xcode 中一样实现 New-File-dialog

ios - Core Data 是否保留 NSMutableArray 中对象的顺序?

swift - NSCalendarUnitDay 将当前日期显示为一位整数 int。如何将其变为 2 位数字,例如 "03"?

swift - NSDateComponents 语法的差异?

ios - 在 Objective C 中,有没有更简洁的方法来测试动态创建的 NSSet 的成员资格?

iphone - 如何检测自定义 UITableviewCell 中的滑动删除手势?

ios - 使用 NSOperationQueue,如何添加到后台队列而不是主队列,以及如何控制操作量?

IOS 日期转换 + 时区问题 + 即使在时区之后也总是返回 -1 天。

iphone - NSDateComponents 给出奇怪的结果