2

さまざまな日付範囲を比較し、範囲間の日数の重複を返す 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;
        }
    }

ただし、エラーは引き続き発生します...日付テキストフィールドが空の場合のみ。コード スニペットの使い方が間違っていますか?

ところで、エラーが発生する「バックトレース」は次のとおりです。

( 0 CoreFoundation 0x01cb0c04 -[ NSCFCalendar components:fromDate:toDate:options:] + 84 1 App
0x00004de6 -[ViewController calculate] + 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
0x01c46e1b CFRunLoopRunInMode + 123 19 GraphicsServices
0x01bfb7e3 GSEventRunModal + 88 20 GraphicsServices
0x01bfb668 GSEventRun + 104 21 UIKit
0x00022ffc UIApplicationMain + 1211 22 App
0x00001e5d main + 141
5d80030 start + 141 5d80030

アップデート:

わかりました、私は自分のコードでいくつかの広範な検索を行い、最終的に、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 の場合に days12 (および result12) のみを計算する if ステートメントを作成しますが、後で date12 変数を使用する必要があるため、機能しませんでした。他の提案はありますか?

4

1 に答える 1

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

助けてくれてありがとう!

于 2013-02-18T20:07:15.517 に答える