在 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/