DatePicker and NSDate crash

I have a mysterious crash when I try to run the application on the iPhone (though it works fine on Simulator). I think the problem is in these 2 methods ..:

-(void)viewWillAppear:(BOOL)animated{ [self getValueFromPicker]; } -(void)getValueFromPicker{ NSDate *now = [NSDate date]; NSDate *birthDate = self.birthdayPicker.date; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now toDate:birthDate options:0]; NSLog(@"Difference in years %i/", components.year); } 

Application error with SIGABRT error, and there is text that I can see in the console: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'

Please help me, I really do not know what I did wrong, when I did the same on the Simulator in the console, I see the difference in years from the date and date of Now, the user selected.

Update: after removal [self defaultBirthdayPickerDate]; in the viewDidLoad section, it starts to work. But now the current date is displayed in the picker, which is not very convenient for choosing a date of birth, now I still need to change the current date from now to the past.

+4
source share
4 answers

These are mainly the types of errors that occur when applying the nil date: check the values ​​of self.birthdayPicker.date , is it nil or / not ?? and to give the proper if condition, you get the nil value of the UIDatePicker.

+7
source

I think the problem is here:

 NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now toDate:birthDate options:0]; 

Your fromDate above toDate .

Change it to:

 NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:birthDate toDate:now options:0]; 
+1
source

I also have a method for setting default values ​​for the collector as follows:

 -(void)defaultBirthdayPickerDate{ NSString *dateString = @"09-Oct-1987"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = @"dd-MMM-yyyy"; NSDate *date = [dateFormatter dateFromString:dateString]; [self.birthdayPicker setDate:date]; } -(void)viewDidLoad{ [self defaultBirthdayPickerDate]; } 

Full console crash report:

 2013-03-05 13:15:12.982 DeathLine[2506:907] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit/UIKit-2380.17/_UIDatePickerView.m:302 2013-03-05 13:15:12.985 DeathLine[2506:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date' *** First throw call stack: (0x325042a3 0x3a22197f 0x3250415d 0x32dd9ab7 0x344bdf15 0x344c6429 0x3458f989 0x35a6b 0x358db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x2908c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0x2dc21 0x3a658b20) libc++abi.dylib: terminate called throwing an exception 0x35a6b 0x358db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x2908c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0x2dc21 0x3a658b20) 2013-03-05 13:15:12.982 DeathLine[2506:907] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit/UIKit-2380.17/_UIDatePickerView.m:302 2013-03-05 13:15:12.985 DeathLine[2506:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date' *** First throw call stack: (0x325042a3 0x3a22197f 0x3250415d 0x32dd9ab7 0x344bdf15 0x344c6429 0x3458f989 0x35a6b 0x358db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x2908c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0x2dc21 0x3a658b20) libc++abi.dylib: terminate called throwing an exception 
0
source

You need to set the locale of your NSDateFormatter. Configure your iPhone settings with US locale, and then set parameter 12/24 for any parameter.
To do this, you need to look into the iphone settings panel.

Hope this works.

0
source

All Articles