-1

I have been trying to know what's wrong with the code as I am getting random crashes on iOS 11:

Fatal Exception: NSRangeException *** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 1]

Here's my code for setting up a datepicker:

func setUpPickerView() {
    let datePickerView = UIDatePicker()
    if let yearDate10 = NSCalendar.current.date(byAdding: .year, value: -10, to: Date()) {
        datePickerView.maximumDate = yearDate10
    }

    if let yearDate20 = NSCalendar.current.date(byAdding: .year, value: -20, to: Date()) {
        datePickerView.date = yearDate20
    }

    if dobTextField != nil {
        dobTextField.inputView = datePickerView
        datePickerView.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
        datePickerView.datePickerMode = .date
    }
}

Crash Logs:

0  CoreFoundation                 0x186bc1d04 __exceptionPreprocess
1  libobjc.A.dylib                0x185e10528 objc_exception_throw
2  CoreFoundation                 0x186b5abd4 _CFArgv
3  CoreFoundation                 0x186a8a9a0 -[__NSArrayM removeObjectAtIndex:]
4  UIKit                          0x19024d2c4 -[UIPickerView selectedRowInComponent:]
5  UIKit                          0x190ac3f24 -[_UIDatePickerMode_Date _dateForYearRow:]
6  UIKit                          0x190ac0ad8 -[_UIDatePickerMode dateForRow:inCalendarUnit:]
7  UIKit                          0x190ac1770 -[_UIDatePickerMode _updateSelectedDateComponentsWithNewValueInComponent:usingSelectionBarValue:]
8  UIKit                          0x190ac1a18 -[_UIDatePickerMode selectedDateComponents]
9  UIKit                          0x190ac408c -[_UIDatePickerMode_Date _shouldEnableValueForRow:inComponent:calendarUnit:]
10 UIKit                          0x190ac2c3c -[_UIDatePickerMode _shouldEnableValueForRow:column:]
11 UIKit                          0x190ac2788 -[_UIDatePickerMode viewForRow:inComponent:reusingView:]
12 UIKit                          0x190ab6380 -[_UIDatePickerView pickerView:viewForRow:forComponent:reusingView:]
13 UIKit                          0x19024d9ec -[UIPickerView tableView:cellForRowAtIndexPath:]
14 UIKit                          0x190aa65d4 -[UIPickerColumnView tableView:cellForRowAtIndexPath:]
15 UIKit                          0x19033393c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
16 UIKit                          0x190333ea0 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]
17 UIKit                          0x1903139e4 -[UITableView _updateVisibleCellsNow:isRecursive:]
18 UIKit                          0x19031df88 -[UITableView _cellForRowAtIndexPath:usingPresentationValues:]
19 UIKit                          0x1901117a4 -[UITableView cellForRowAtIndexPath:]
20 UIKit                          0x1908e6c80 __61-[UIPickerTableView selectRow:animated:notify:updateChecked:]_block_invoke
21 Foundation                     0x187565b70 __NSIndexSetEnumerate
22 UIKit                          0x1908e691c -[UIPickerTableView selectRow:animated:notify:updateChecked:]
23 UIKit                          0x19024d618 -[UIPickerView _selectRow:inComponent:animated:notify:]
24 UIKit                          0x190ab49e4 -[_UIDatePickerView _selectRow:inComponent:animated:notify:]
25 UIKit                          0x190ac13f4 -[_UIDatePickerMode loadDate:animated:]
26 UIKit                          0x190ab4998 -[_UIDatePickerView _loadDate:animated:]
27 UIKit                          0x190ab4020 -[_UIDatePickerView _setDate:animated:forced:]
28 UIKit                          0x190ab4578 -[_UIDatePickerView _setMode:]
29 UIKit                          0x190ab4694 -[_UIDatePickerView setDatePickerMode:]
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
jatin K
  • 55
  • 5
  • handleDatePicker(sender:) ? – El Tomato Jan 25 '18 at 07:41
  • Check if your datePickerView.maximumdate > datePickerView.date. – Teetz Jan 25 '18 at 07:41
  • 1
    Are you pretty sure that the reason of the crash is caused by the mentioned code? – Ahmad F Jan 25 '18 at 07:48
  • 1
    This looks like some array is out of bounds. I don't think this error has to do something with your posted code. – Au Ris Jan 25 '18 at 08:09
  • @AhmadF yes the crash logs: – jatin K Jan 25 '18 at 08:19
  • 2
    @jatinK The Crashlog doesn't say that UIDatePicker is crashing. It is saing that in your `selectedRowInComponent` method, you are removing an object from an Array by `removeObjectAtIndex`, and that index happens to be `9223372036854775807 ` while the array is only two elements strong so valid indexes are only 0 and 1. That's why you get an array out of bounds exception. It doesn't have anything to do with UIDatePicker or iOS11, rather your own implementation. Add an All Exceptions breakpoint, run the use case, see where the exception occurs, figure out why and fix it. – NSNoob Jan 25 '18 at 08:49
  • @NSNoob Its something with datepicker mode can you help me in that as i am not selecting any row of the datepicker – jatin K Jan 25 '18 at 08:54
  • @jatinK I already did. Read the last line, that will lead you directly to the line where the crash occurs and then you can figure out why it happened. – NSNoob Jan 25 '18 at 08:55
  • @NSNoob i added the all exception breakpoint but there is no exception while i play with the app while debugging. but anyways i get this crash on fabric – jatin K Jan 25 '18 at 09:03
  • @jatinK Well mate that's all anyone can tell you given the limited information you have provided us. You don't know the reproduction scenario and you don't know if you are removing something from some array somwhere. Don't see how could anyone pinpoint you to the issue. – NSNoob Jan 25 '18 at 09:06
  • 1
    https://stackoverflow.com/questions/46964869/uidatepicker-nsrangeexception-crash-ios-11 https://stackoverflow.com/questions/46600674/iosfatal-exception-nsrangeexception ? – Larme Jan 25 '18 at 10:02
  • @jatinK Did you get fix of this. I am also facing similar issue. Unable to reproduce it but getting rash on Fabrics. – iKushal Jul 16 '18 at 05:15

1 Answers1

0

Your datePickerView is created in the function. As soon as setUpPickerView returns, that variable is released.

You will need to have the datePickerView variable as part of your view controller, like so:

class MyViewController: UIViewController {
    var datePickerView: UIPickerView?

    func setUpPickerView() {
        self.datePickerView = UIDatePicker()
        ...
    }
}
IanSabine
  • 54
  • 5