NSDateComponents on IOS6 not working?

↘锁芯ラ 提交于 2019-12-14 04:22:49

问题


I have a simple program which was working with IOS4 and 5. Now with IOS 6 I'm getting the following error message:

2012-10-05 11:08:16.386 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:18.072 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.273 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.274 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.275 app[1698:19d03] 0
2012-10-05 11:08:25.055 app[1698:19d03] 2011-10-05 09:08:11 +0000
2012-10-05 11:08:25.823 app[1698:19d03] (
"<UILongPressGestureRecognizer: 0xa167f60; state = Possible; view = <UITableViewCellContentView 0xa168000>; target= <(action=_longPressGestureRecognized:, target=<UIPickerTableViewWrapperCell 0xa168090>)>>")
2012-10-05 11:08:25.824 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:25.824 app[1698:19d03] -[__NSArrayI timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x94ef5d0
2012-10-05 11:08:25.825 app[1698:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x94ef5d0'
*** First throw call stack:
(0x1d16012 0x11fae7e 0x1da14bd 0x1d05bbc 0x1d0594e 0x1d25cd3 0xa092 0x120e705 0x145920 0x1458b8 0x206671 0x206bcf 0x205d38 0x17533f 0x175552 0x1533aa 0x144cf8 0x1f4fdf9 0x1f4fad0 0x1c8bbf5 0x1c8b962 0x1cbcbb6 0x1cbbf44 0x1cbbe1b 0x1f4e7e3 0x1f4e668 0x14265c 0x296d 0x28a5)

The first run is with two dates which are equal. On the second run, I've changed the fhDate on the picker as you can see...

The code is simplified a bit for easier reading:

aiDate = [adoIgazPicker date];
NSLog(@"%@", aiDate);
fhDate = [forgHelyPicker date];
NSLog(@"%@", fhDate);
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = fhDate;
NSDate *endDate = aiDate;
NSLog(@"%@", startDate);
NSLog(@"%@", endDate);
NSDateComponents *comps = [gregorian components:NSMonthCalendarUnit fromDate:startDate toDate:endDate options:0]; //THIS LINE FAILS IF THE DATES ARE NOT THE SAME
int months = [comps month];
NSLog(@"%i", months);

Thank you for your help!


回答1:


I've replaced

NSDateComponents *comps = [gregorian components:NSMonthCalendarUnit fromDate:startDate toDate:endDate options:0];

with

NSInteger months = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit
                                                    fromDate: startDate
                                                      toDate: endDate
                                                     options: 0] month];

and now it's working :).



来源:https://stackoverflow.com/questions/12743198/nsdatecomponents-on-ios6-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!