9

I have the following code to add a DatePicker to one of my UIViews.

UIDatePicker *datePicker =
      [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 30, 320, 250)];
  [datePicker setDatePickerMode:UIDatePickerModeDate];
  datePicker.hidden = NO;
  datePicker.date = [NSDate date];
  [datePicker addTarget:self
                 action:@selector(changeDateInLabel:)
       forControlEvents:UIControlEventValueChanged];
  [self.dateView addSubview:datePicker];

This code has been around for a while but we've been noticing some random crashes now. The crashes only happen in iOS 11. This is what the stack trace of the crash looks like

Fatal Exception: NSRangeException
    *** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 1]    
    Fatal Exception: NSRangeException
        0  CoreFoundation                     0x180d87d38 __exceptionPreprocess
        1  libobjc.A.dylib                    0x18029c528 objc_exception_throw
        2  CoreFoundation                     0x180d20c44 _CFArgv
        3  CoreFoundation                     0x180c50cc0 -[__NSArrayM removeObjectAtIndex:]
        4  UIKit                              0x18a3ecaa8 -[UIPickerView selectedRowInComponent:]
        5  UIKit                              0x18ac52224 -[_UIDatePickerMode_Date _dateForYearRow:]
        6  UIKit                              0x18ac4edd8 -[_UIDatePickerMode dateForRow:inCalendarUnit:]
        7  UIKit                              0x18ac4fa70 -[_UIDatePickerMode _updateSelectedDateComponentsWithNewValueInComponent:usingSelectionBarValue:]
        8  UIKit                              0x18ac4fd18 -[_UIDatePickerMode selectedDateComponents]
        9  UIKit                              0x18ac43370 -[_UIDatePickerView _updatedLastSelectedComponentsByValidatingSelectedDateWithLastManipulatedComponent:]
        10 UIKit                              0x18ac427e8 -[_UIDatePickerView _setDate:animated:forced:]
        11 UIKit                              0x18ac42d24 -[_UIDatePickerView _setMode:]
        12 UIKit                              0x18ac42e40 -[_UIDatePickerView setDatePickerMode:]

Any pointers on what would be causing this crash? Thank you

tbag
  • 1,268
  • 2
  • 16
  • 34
  • `index 9223372036854775807 beyond bounds [0 .. 1]` seems pretty clear to me. How do you try to access the picker's data? – LinusGeffarth Oct 26 '17 at 22:39
  • What's the implementation of `changeDateInLabel:`? Just as Linus said, the error is pretty self-explanatory. – Alejandro Iván Oct 26 '17 at 22:43
  • 3
    Honestly, this looks like a framework bug to me, if the crash is occurring on the second line of the code above (which the stack trace suggests, but which could be verified either using the debugger or by sticking a log after the `-setDatePickerMode:` line and seeing if that log occurs before the crash). A brand-new date picker shouldn't be crashing just because you set the mode on it. With that said, maybe if you reorder your lines so that you call `-setDatePickerMode:` after you set everything else up, it will work around it? – Charles Srstka Oct 26 '17 at 22:46
  • 1
    @CharlesSrstka I agree. I would try setting the picker's `date` property before setting the picker mode and see if that helps. – rmaddy Oct 26 '17 at 22:58
  • @LinusGeffarth The crash happens on viewdidload where I setup the date picker not when I try to access the picker data – tbag Oct 26 '17 at 23:04
  • @CharlesSrstka Thanks, that's what I feel too. I'll reorder it so the datePickerMode is set up at the end. Also, I'm thinking of setting a min and max date for the picker. I'm not able to reproduce the crash so will add some crashlytics breadcrumbs and monitor it. – tbag Oct 26 '17 at 23:07
  • 1
    What I'd do would be to try to reduce it to the simplest possible program that will cause the crash, and attach the project to a Radar report. – Charles Srstka Oct 26 '17 at 23:15
  • 2
    Agree it's a framework bug, based on the symbolic name I'd suspect it's trying to validate something that no longer applies in iOS 11. Does it still crash if you delete and reinstall the app (i.e. fresh iOS 11 install?)?. – kvr Oct 29 '17 at 01:38
  • Seems related: https://stackoverflow.com/questions/46600674/iosfatal-exception-nsrangeexception Maybe a Radar should be opened? – Larme Oct 31 '17 at 06:25
  • @tbag Did you get any solution for this? – iKushal Jul 16 '18 at 05:33

1 Answers1

-2

Follow this code.it is help you.

UIDatePicker *datePicker=[[UIDatePicker alloc]init];
    datePicker.datePickerMode=UIDatePickerModeDate;
    [self.dateSelectionTextField setInputView:datePicker];
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolBar setTintColor:[UIColor grayColor]];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)];
    UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
    [self.dateSelectionTextField setInputAccessoryView:toolBar];.

-(void)ShowSelectedDate
{   NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MMM/YYYY hh:min a"];
    self.dateSelectionTextField.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]];
    [self.dateSelectionTextField resignFirstResponder];
}