UIDatePicker setDatePickerMode Crash in iOS14.7.1

I have the same problem, link: https://stackoverflow.com/questions/49770654/uidatepicker-nsinternalinconsistencyexception-unexpected-number-of-calendar-uni

The first answer cannot fix this bug. Help, please

Crash report below:


Fatal Exception: NSInternalInconsistencyException unexpected number of calendar units: 4 for format: EEE ├'day': d┤ HH.mm (expecting at least 5 elements)

Fatal Exception: NSInternalInconsistencyException

0 CoreFoundation 0x184bb5d04 __exceptionPreprocess

1 libobjc.A.dylib 0x183e04528 objc_exception_throw

2 CoreFoundation 0x184bb5bd8 +[NSException raise:format:]

3 Foundation 0x185545c24 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]

4 UIKit 0x18eab9b38 -[_UIDatePickerMode_DateAndTime elements]

5 UIKit 0x18eab3898 -[_UIDatePickerMode displayedCalendarUnits]

6 UIKit 0x18eab98f8 -[_UIDatePickerMode_DateAndTime displayedCalendarUnits]

7 UIKit 0x18eaa8448 -[_UIDatePickerView _setMode:]

8 UIKit 0x18eaa8694 -[_UIDatePickerView setDatePickerMode:]

9 UIKit 0x18e55ac68 -[UIDatePicker initWithCoder:]

10 UIKit 0x18e727160 UINibDecoderDecodeObjectForValue

11 UIKit 0x18e726e98 -[UINibDecoder decodeObjectForKey:]

12 UIKit 0x18e57c0cc -[UIRuntimeConnection initWithCoder:]

13 UIKit 0x18e727160 UINibDecoderDecodeObjectForValue

14 UIKit 0x18e7272d8 UINibDecoderDecodeObjectForValue

15 UIKit 0x18e726e98 -[UINibDecoder decodeObjectForKey:]

16 UIKit 0x18e57b40c -[UINib instantiateWithOwner:options:]

17 UIKit 0x18e35b17c -[UIViewController _loadViewFromNibNamed:bundle:]

18 UIKit 0x18e10aee4 -[UIViewController loadView]

19 UIKit 0x18dfecba4 -[UIViewController loadViewIfRequired]

20 UIKit 0x18dfecad4 -[UIViewController view]

21 UIKit 0x18e171680 -[UINavigationController _startCustomTransition:]

22 UIKit 0x18e0932c8 -[UINavigationController _startDeferredTransitionIfNeeded:]

23 UIKit 0x18e092f0c -[UINavigationController __viewWillLayoutSubviews]

24 UIKit 0x18e092e0c -[UILayoutContainerView layoutSubviews]

25 UIKit 0x1a3a651b8 -[UILayoutContainerViewAccessibility layoutSubviews]

26 UIKit 0x18dfea2f8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]

27 QuartzCore 0x188ba3ec8 -[CALayer layoutSublayers]

28 QuartzCore 0x188ba7fa8 CA::Layer::layout_if_needed(CA::Transaction*)

29 QuartzCore 0x188b16a98 CA::Context::commit_transaction(CA::Transaction*)

30 QuartzCore 0x188b3ceb4 CA::Transaction::commit()

31 QuartzCore 0x188b3dcf4 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)

32 CoreFoundation 0x184b5d848 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION

33 CoreFoundation 0x184b5b200 __CFRunLoopDoObservers

34 CoreFoundation 0x184b5b7bc __CFRunLoopRun

35 CoreFoundation 0x184a7bfb8 CFRunLoopRunSpecific

36 GraphicsServices 0x186913f84 GSEventRunModal

37 UIKit 0x18e0502e8 UIApplicationMain

38 AwesomeApp 0x100da4924 main (AppDelegate.swift:18)

39 libdyld.dylib 0x18459e56c start

Error message:

unexpected number of calendar units: 4 for format: EEE ├'day': d┤ HH.mm (expecting at least 5 elements)

Could you show how you have defined format ?

You should have something like this:

let source = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let day = dateFormatter.date(from: source)
print("day", day!)

Or (with just 5 components):

let source = "Thu, 14 Oct 2021 10:26:45"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss"
let day = dateFormatter.date(from: source2)
print("day", day!)

Take care that the 3 letters for the day of week must correspond to locale:

  • "Thu" if "en_US"
  • "Jeu" if "fr_FR"
  • "Jue" if "es_ES"

It crashes otherwise.

This is my code, I don't have set dateFormatter.

- (UIDatePicker *)datePicker
{
  if (!_datePicker) {
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
    if (@available(iOS 13.4, *)) {
      _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
    }
    NSDate *date = [NSDate date];
    _datePicker.locale = [NSLocale currentLocale];
    [_datePicker setDatePickerMode:UIDatePickerModeDate];       // crash
    [_datePicker setMaximumDate:date];
  }
  return _datePicker;
}
UIDatePicker setDatePickerMode Crash in iOS14.7.1
 
 
Q