我创建了一个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;
}
}
但是,仅当日期文本字段为空时,错误仍会发生。我是否以错误的方式使用代码段?
BTW,这是发生错误的“回溯”:
(0 CoreFoundation 0x01cb0c04 - [ NSCFCalendar 组件:fromDate:toDate:options:] + 84 1 App
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(Internal)_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 GraphicsServices
0x01bfcdf9 _PurpleEventCallback + 339 13 GraphicsServices
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 CoreFoundation
0x01c46e1b CFRunLoopRunInMode + 123 19 GraphicsServices
0x01bfb7e3 GSEventRunModal + 88 20图形服务
0x01bfb668 GSEventRun + 104 21 UIKit
0x00022ffc UIApplicationMain + 1211 22 App
0x00001e5d main + 141 23 App
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>我会创建一个if语句来只计算days12(和result12)。 0,但我必须稍后使用date12变量,所以它不起作用。还有其他建议吗?
答案 0 :(得分:0)
好吧,我明白了。以下是我修复它的方法:
int days12;
if ([value1 length] == 0)
{
days12 = 0;
result12.text= [[NSString alloc] initWithFormat:@"%i", days12];
}
else
{
days12 = [[[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startdate1 toDate:enddate2 options:0] day] + 1;
result12.text = [[NSString alloc] initWithFormat:@"%i", days12];
}
感谢您的帮助!