我从 Crashlytic 收到了这份崩溃报告,其中指出 UIDatePicker 中存在错误。它是iOS中的错误吗?因为似乎是仅在 AppDelegate 中触及我的应用程序的入口点。
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
最佳答案
不幸的是,这是自 iOS 11 以来 UIDatePicker
的错误。当设备处于特定区域和日历设置时,它会发生。我可以重现的场景之一是:
- 地区:印度尼西亚
- 日历:佛教
解决此问题的一种方法是将日历专门设置为您想要的格式。
let datePicker = UIDatePicker()
datePicker.calendar = .gregorian
用于轻松访问 UIDatePicker
的所有用法。您可以像这样创建一个扩展:
extension UIDatePicker {
static func makeDatePicker(mode: UIDatePicker.Mode, maximumDate: Date? = nil) -> UIDatePicker {
let datePicker = UIDatePicker()
datePicker.calendar = .gregorian
datePicker.datePickerMode = mode
datePicker.maximumDate = maximumDate
return datePicker
}
}
另外不要忘记将它也带到您的 DateFormatter
中。当DateFormatter
启动时,它将使用设备默认日历。这是可以实现的:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.calendar = .gregorian
let dateString = dateFormatter.string(from: Date())
关于ios - UIDatePicker NSInternalInconsistencyException : unexpected number of calendar units error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49770654/