【发布时间】:2013-02-18 02:42:36
【问题描述】:
我创建了一个 iOS 应用程序,它比较各种日期范围并返回范围之间的天数重叠。该应用程序按预期工作,但是当我在模拟器的用户界面中点击“计算”按钮时,控制台返回以下文本(摘录):
*** -[__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil
I mean really, what do you think that operation is supposed to mean with a nil fromDate?
An exception has been avoided for now.
A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.
Here is the backtrace where this occurred this time (some frames may be missing due to compiler optimizations):
应用程序不会崩溃,Xcode 也不会检测到任何问题。不过,我宁愿不要忽视这一点。有谁知道发生了什么?这是我的代码:
NSDate *overlapFrom12_range1 = [range1Start laterDate:startdate1];
NSDate *overlapTo12_range1 = [range1End earlierDate:enddate2];
NSInteger days12_range1;
if ([overlapFrom12_range1 compare:overlapTo12_range1] > 0)
{
days12_range1 = 0;
}
else
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSDayCalendarUnit fromDate:overlapFrom12_range1 toDate:overlapTo12_range1 options:0];
days12_range1 = [comp day] +1;
}
我将您的建议添加到我的代码中,如下所示:
NSInteger days12_range1;
if ([overlapFrom12_range1 compare:overlapTo12_range1] > 0)
{
days12_range1 = 0;
}
else
{
if ([date1.text length] > 0 && [date2.text length] > 0)
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSDayCalendarUnit fromDate:overlapFrom12_range1 toDate:overlapTo12_range1 options:0];
days12_range1 = [comp day] +1;
}
}
但是,错误仍然发生...仅当日期文本字段为空时。我是不是用错了sn-p代码?
顺便说一句,这是发生错误的“回溯”:
( 0 CoreFoundation 0x01cb0c04 -[NSCFCalendar 组件:从日期:到日期:选项:] + 84 1 应用程序
0x00004de6 -[ViewController 计算] + 598 2 libobjc.A.dylib
0x010f2705-[NSObject performSelector:withObject:withObject:] + 77 3 UIKit 0x000262c0 -[UIApplication sendAction:to:from:forEvent:] + 96 4 UIKit
0x00026258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 5 UIKit 0x000e7021-[UIControl sendAction:to:forEvent:] + 66 6 UIKit
0x000e757f -[UIControl(内部)_sendActionsForEvents:withEvent:] + 578 7 UIKit 0x000e66e8-[UIControl touchesEnded:withEvent:] + 546 8 UIKit
0x00055cef -[UIWindow _sendTouchesForEvent:] + 846 9 UIKit
0x00055f02 -[UIWindow sendEvent:] + 273 10 UIKit
0x00033d4a -[UIApplication sendEvent:] + 436 11 UIKit
0x00025698 _UIApplicationHandleEvent + 9874 12 图形服务
0x01bfcdf9 _PurpleEventCallback + 339 13 图形服务
0x01bfcad0 PurpleEventCallback + 46 14 CoreFoundation
0x01c16bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 53 15 CoreFoundation 0x01c16962 __CFRunLoopDoSource1 + 146 16 CoreFoundation 0x01c47bb6 __CFRunLoopRun + 2118 17 CoreFoundation
0x01c46f44 CFRunLoopRunSpecific + 276 18 核心基础
0x01c46e1b CFRunLoopRunInMode + 123 19 图形服务
0x01bfb7e3 GSEventRunModal + 88 20 图形服务
0x01bfb668 GSEventRun + 104 21 UIKit
0x00022ffc UIApplicationMain + 1211 22 应用程序
0x00001e5d 主 + 141 23 应用
0x00001d85 开始 + 53 )
更新:
好的,我在我的代码中进行了一些广泛的搜索,最后,使用 NSLog,我能够查明受影响的区域。这是导致问题的代码:
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; //allocate and initialize dateformatter1
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"]; //set date format using dateformatter1
NSDate *startdate1 = [dateFormatter1 dateFromString: date1.text]; //set startdate
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init]; //allocate and initialize dateformatter2
[dateFormatter2 setDateFormat:@"MM/dd/yyyy"]; //set date format using dateformatter2
NSDate *enddate2 = [dateFormatter2 dateFromString: date2.text]; //set enddate
int days12 = [[[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startdate1 toDate:enddate2 options:0] day] + 1;
result12.text = [[NSString alloc] initWithFormat:@"%i", days12];
NSLog (@"Startdate1 is %@ and Enddate2 is %@.", startdate1, enddate2);
NSLog 显示 startdate1 和 enddate2 都是(null)。如果 startdate1 和 enddate2 > 0,我将创建一个 if 语句来仅计算 days12(和 result12),但我必须稍后使用 date12 变量,所以它不起作用。还有其他建议吗?
【问题讨论】:
-
那么,overlapFrom12_range1 有值吗?你 NSLog 了吗?
-
哇,我对错误消息感到非常惊讶和印象深刻,我不得不在推特上发布它和这个问题!
-
顺便说一句。该代码看起来像我对您的问题stackoverflow.com/questions/14671078/… 的回答,因此如果有问题,您也可以对此做出回应(或者如果没有问题,则接受答案)。
-
是的,这是你的答案。谢谢!我将检查上述副本是否对我有用。 :)
标签: ios objective-c xcode