Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 日期计算中的奇怪错误_Ios_Objective C_Xcode - Fatal编程技术网

Ios 日期计算中的奇怪错误

Ios 日期计算中的奇怪错误,ios,objective-c,xcode,Ios,Objective C,Xcode,我创建了一个iOS应用程序,它可以比较不同的日期范围,并返回日期范围之间的重叠天数。应用程序按预期工作,但当我点击模拟器用户界面中的“计算”按钮时,控制台返回以下文本(节选): 应用程序不会崩溃,Xcode也不会检测到任何问题。不过,我还是不想忽视这一点。有人知道发生了什么事吗?这是我的密码: NSDate *overlapFrom12_range1 = [range1Start laterDate:startdate1]; NSDate *overlapTo12_range1 =

我创建了一个iOS应用程序,它可以比较不同的日期范围,并返回日期范围之间的重叠天数。应用程序按预期工作,但当我点击模拟器用户界面中的“计算”按钮时,控制台返回以下文本(节选):

应用程序不会崩溃,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;
        }
    }
但是,错误仍然发生。。。仅当日期文本字段为空时。我是否用错了代码段

顺便说一句,这里是发生错误的“回溯”:

(0 CoreFoundation 0x01cb0c04-[NSCFCalendar 组件:fromDate:toDate:options:+84 1应用程序
0x00004de6-[ViewController calculate]+598 2 libobjc.A.dylib
0x010f2705-[NSObject性能选择器:withObject:withObject:+77 3 UIKit 0x000262c0-[UIKit应用程序 sendAction:to:from:forEvent:+96 4 UIKit
0x00026258-[UIApplication sendAction:toTarget:fromSender:forEvent:]+ 61 5 UIKit 0x000e7021-[UIControl 发送操作:发送到:forEvent:+66 6 UIKit
0x000e757f-[UIControl(内部)\发送操作预防:带事件:]+ 578 7 UIKit 0x000e66e8-[UIControl touchesEnded:withEvent:+546 8 UIKit
0x00055cef-[UIWindow\u sendTouchesForEvent:+846 9 UIKit
0x00055f02-[UIWindow sendEvent:+273 10 UIKit
0x00033d4a-[UIApplication sendEvent:+436 11 UIKit
0x00025698\u UIApplicationHandleEvent+9874 12图形服务
0x01bfcdf9_PurpleEventCallback+339 13图形服务
0x01bfcad0紫背风+46 14芯基础
0x01c16bf5\uu CFRUNLOOP\u正在调用\u OUT\u到\u SOURCE1\u PERFORM\u函数
+53 15 CoreFoundation 0x01c16962 _CFRunLoopDoSource1+146 16 CoreFoundation 0x01c47bb6 __CFRunLoopRun+2118 17 CoreFoundation
0x01c46f44 CFRunLoopRunSpecific+276 18核心基础
0x01c46e1b CFRUNLOOPSRUNINMODE+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变量,因此它不起作用。还有其他建议吗?

好的,我想好了。下面是我如何修复它的:

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

谢谢你的帮助

那么,从12_range1重叠是否有一个值?你把它记录下来了吗?哇,我对这个错误消息感到非常惊讶和印象深刻,我不得不在推特上发布它和这个问题!顺便说一句,代码看起来像是我对你问题的回答,因此如果有问题,你也可以回答(如果没有问题,也可以接受答案)。是的,这是你的答案。谢谢我将检查上述副本是否适用于我。:)
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];
    }