Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange bug during Date Calculation
    text
    copied!<p>I created an iOS app that compares various date ranges and returns the overlap of days between the ranges. The app is working as intended, but when I hit the "Calculate" button in the user interface of the simulator, the console returns the following text (excerpt):</p> <pre><code> *** -[__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): </code></pre> <p>The app does not crash, nor does Xcode detect any issues. Still, I would rather not ignore this. Does anyone know what is going on? Here is my code:</p> <pre><code>NSDate *overlapFrom12_range1 = [range1Start laterDate:startdate1]; NSDate *overlapTo12_range1 = [range1End earlierDate:enddate2]; NSInteger days12_range1; if ([overlapFrom12_range1 compare:overlapTo12_range1] &gt; 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; } </code></pre> <hr> <p>I added your suggestion to my code like so:</p> <pre><code> NSInteger days12_range1; if ([overlapFrom12_range1 compare:overlapTo12_range1] &gt; 0) { days12_range1 = 0; } else { if ([date1.text length] &gt; 0 &amp;&amp; [date2.text length] &gt; 0) { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comp = [calendar components:NSDayCalendarUnit fromDate:overlapFrom12_range1 toDate:overlapTo12_range1 options:0]; days12_range1 = [comp day] +1; } } </code></pre> <p>However, the error still occurs... only if the date textfields are empty. Am I using the code snippet the wrong way?</p> <p>BTW, here is the "backtrace" where the error occurs:</p> <blockquote> <p>( 0 CoreFoundation 0x01cb0c04 -[<strong>NSCFCalendar components:fromDate:toDate:options:] + 84 1 App<br> 0x00004de6 -[ViewController calculate] + 598 2 libobjc.A.dylib<br> 0x010f2705 -[NSObject performSelector:withObject:withObject:] + 77 3 UIKit 0x000262c0 -[UIApplication sendAction:to:from:forEvent:] + 96 4 UIKit<br> 0x00026258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 5 UIKit 0x000e7021 -[UIControl sendAction:to:forEvent:] + 66 6 UIKit<br> 0x000e757f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578 7 UIKit 0x000e66e8 -[UIControl touchesEnded:withEvent:] + 546 8 UIKit<br> 0x00055cef -[UIWindow _sendTouchesForEvent:] + 846 9 UIKit<br> 0x00055f02 -[UIWindow sendEvent:] + 273 10 UIKit<br> 0x00033d4a -[UIApplication sendEvent:] + 436 11 UIKit<br> 0x00025698 _UIApplicationHandleEvent + 9874 12 GraphicsServices<br> 0x01bfcdf9 _PurpleEventCallback + 339 13 GraphicsServices<br> 0x01bfcad0 PurpleEventCallback + 46 14 CoreFoundation<br> 0x01c16bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION</strong> + 53 15 CoreFoundation 0x01c16962 __CFRunLoopDoSource1 + 146 16 CoreFoundation 0x01c47bb6 __CFRunLoopRun + 2118 17 CoreFoundation<br> 0x01c46f44 CFRunLoopRunSpecific + 276 18 CoreFoundation<br> 0x01c46e1b CFRunLoopRunInMode + 123 19 GraphicsServices<br> 0x01bfb7e3 GSEventRunModal + 88 20 GraphicsServices<br> 0x01bfb668 GSEventRun + 104 21 UIKit<br> 0x00022ffc UIApplicationMain + 1211 22 App<br> 0x00001e5d main + 141 23 App<br> 0x00001d85 start + 53 )</p> </blockquote> <p>UPDATE:</p> <p>Ok, I did some extensive searching in my code, and finally, with NSLog, I was able to pinpoint the affected area. Here is the code that is causing the trouble:</p> <pre><code>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); </code></pre> <p>NSLog shows that both startdate1 and enddate2 are (null). I would create an if-statement to only calculate days12 (and result12) if startdate1 and enddate2 > 0, but I have to use the date12 variable later on, so it did not work. Any other suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload