Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Crash with DatePicker and NSDate

I have mysterious crash when trying to launch my app on iPhone (it perfectly valid works on Simulator though). I think problem is in this 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);

}

App crash with SIGABRT error, and there is a text i can see in a console: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'

Please help me, i'am really have no idea what i did wrong, when i did the same thing on Simulator in console i can see difference in years from Now date and date, user picked.

Update:after i removed [self defaultBirthdayPickerDate]; in viewDidLoad section it start to work. But, now picker show current date, its not very convenient for picking birthday Date, now i still need to change its current date from now to past..

Upvotes: 4

Views: 5548

Answers (4)

iPatel
iPatel

Reputation: 47049

Mostly this types of error generated when you applies nil date: check values of self.birthdayPicker.date it is nil or/not ?? and give proper condition if you get nil value of UIDatePicker.

Upvotes: 8

AppleDelegate
AppleDelegate

Reputation: 4249

You need to set the locale of your NSDateFormatter. Set up your iPhone settings with US locale and then set the 12/24 setting to any one option.
For that you have to look into the iphone settings panel.

Hope this works.

Upvotes: 1

Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

I also have a method for setting default values for picker like this:

-(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 crash report in console is:

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

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107121

I think the issue is here:

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

Your fromDate is higher than toDate.

Change it to:

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

Upvotes: 1

Related Questions