IOS6上的NSDateComponents无法正常工作? [英] NSDateComponents on IOS6 not working?

查看:114
本文介绍了IOS6上的NSDateComponents无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,它正在使用IOS4和5.
现在使用IOS 6我收到以下错误消息:

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)

第一次运行是两个相等的日期。
在第二次运行中,我已经改变了选择器上的fhDate,你可以看到...

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);

感谢您的帮助!

推荐答案

我已经替换了

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];

现在正在运作:)。

这篇关于IOS6上的NSDateComponents无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆