1
var datePickerView: UIDatePicker = UIDatePicker() 
override func viewDidLoad() {
super.viewDidLoad()
initializeBarButtons() }
func initializeBarButtons() {  
datePickerView.datePickerMode = UIDatePickerMode.date // This is the line that gets the error
datePickerView.addTarget(self, action: #selector(EmployeeDetailEditViewController.handleDatePicker), for: UIControlEvents.valueChanged) 
  }

This commented line is causing the crash when the calendar is changed from Gregorian to Buddhist.

Crash : -

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 1]' * First throw call stack: (0x18626e364 0x1854b4528 0x186206e9c 0x186136840 0x18f9a1890 0x1902238a8 0x19022045c 0x1902210f4 0x19022139c 0x190214254 0x1902136cc 0x190213c08 0x190213d24 0x1013881e0 0x101386d1c 0x101387018 0x18f768590 0x18f768304 0x18f8d2bd4 0x18f7f5c4c 0x18f7f5890 0x18f7f5790 0x1a53e4c18 0x18f74cf00 0x18a2b1998 0x18a2b5b20 0x18a22236c 0x18a249b90 0x18a24a9d0 0x186215edc 0x186213894 0x186213e50 0x186133e58 0x187fe0f84 0x18f7b367c 0x10131b378 0x185c5056c) libc++abi.dylib: terminating with uncaught exception of type NSException

How to Solve This ?

Amey
  • 795
  • 8
  • 31
  • First you need to show more code so that we can see the use of the line in context. It's very difficult to look at a single (or two) line of code and tell what the error is. – Upholder Of Truth Jan 15 '18 at 11:04
  • Updated @UpholderOfTruth – Amey Jan 15 '18 at 11:10
  • I have edited the question to fix the code formatting for you. However what I would really need to see is how the date picker itself is setup. – Upholder Of Truth Jan 15 '18 at 11:14
  • i have edited Code Any More info needed @UpholderOfTruth – Amey Jan 15 '18 at 11:36
  • Do you do nothing else with the picker like add it to a view, configure any other settings. If I just create a default picker, add it to my view and set it's frame I works with the Gregorian and Buddhist calendars. Are you changing the calendar in the iOS settings before running the app? – Upholder Of Truth Jan 15 '18 at 11:46
  • after running the app Changing the calendar and same issue is with before changing the calendar . It works perfectly with ios 10 but ios 11 causes crash i have also included the selector function . @UpholderOfTruth – Amey Jan 15 '18 at 11:50
  • I think you need to show every bit of code related to the date picker. How are you adding it to a view to be displayed? Are you configuring anything else about it because I can't get it to go wrong. – Upholder Of Truth Jan 15 '18 at 11:52
  • textField.inputView = datePickerView Just Set this @UpholderOfTruth – Amey Jan 15 '18 at 11:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163183/discussion-between-amey-and-upholder-of-truth). – Amey Jan 15 '18 at 12:05
  • Ok I created a simple test project with a single `UITextField` and setup as you have said. I run it for the Gregorian calendar then quit and run it again with the Buddhist calendar and it works for both. I tried this in the simulator for iPhone 8, iPhone X and iPad. – Upholder Of Truth Jan 15 '18 at 12:06
  • Crash is not occurring on simulator . Please check on actual device . @UpholderOfTruth – Amey Jan 15 '18 at 12:11
  • Ok I'm in the office at the moment so the time I have devote to this is low but hopefully we have added some extra details that will help get the question answered. I will look into it further when I get time. – Upholder Of Truth Jan 15 '18 at 12:13
  • ok . Pls Update if any solution found @UpholderOfTruth – Amey Jan 15 '18 at 12:17
  • 1
    @UpholderOfTruth did you find any solution since I am also getting the same crash? – atul Jan 19 '18 at 12:55
  • Sorry I haven't had time to test it as I've spent the last few days downloading and installing over 40GB of test data in the simulator which at some point I need to upload to my iPhone (neither work iPads have enough storage). I will try to take another look today. – Upholder Of Truth Jan 19 '18 at 13:00
  • @atul I've just tried my test app on my iPhone 8 and it worked fine. Do you have a test project you could share that has the problem (dropbox, git, etc) – Upholder Of Truth Jan 19 '18 at 13:06
  • 1
    Hi guys, any solution around this? – Rohit Feb 16 '18 at 07:20
  • 1
    @Rohit i used if #available(iOS 11.0, *) { let gregorianCalendar = Calendar(identifier: .gregorian) self.datePickerView.calendar = gregorianCalendar } else { // Fallback on earlier versions } . Temporary Solution To Forcefully Set calendar as gregorian – Amey Feb 16 '18 at 08:23
  • @Amey but why we needed the check for iOS 11? As gregorianCalendar is available in iOS 7+ – Rohit Feb 16 '18 at 09:43
  • I added Because that line was causing crash in Ios 11 only . So to set calendar forcefully in ios 11 to gregorian @Rohit – Amey Feb 16 '18 at 12:32

1 Answers1

1

Unfortunately, you can't solve it, as there is an open radar on it (https://openradar.appspot.com/41120005). Meanwhile, use @Amey workaround:

if #available(iOS 11.0, *) { 
    let gregorianCalendar = Calendar(identifier: .gregorian) 
    self.datePickerView.calendar = gregorianCalendar 
} else { 
    // Fallback on earlier versions 
}
yonivav
  • 994
  • 11
  • 18
  • this doesn't work. still crashing on datePickerView.datePickerMode = UIDatePickerMode.date when phone calendar is set to Buddhist – Usama Sadiq Oct 05 '21 at 03:33
  • I was facing a crash when the region is Singapore and the calendar is Buddhist. when its United states and Buddhist there was not crash. – Usama Sadiq Oct 06 '21 at 03:24
  • datePicker.locale = Locale(identifier: "en_US_POSIX") I was missing this line now region is standardised – Usama Sadiq Oct 06 '21 at 03:25