UIDatePicker NSInternalInconsistencyException:日历单元的意外数量错误

问题描述 投票:2回答:1

我从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 swift uikit crashlytics uidatepicker
1个回答
0
投票

[不幸的是,这是自iOS 11起UIDatePicker的错误。它发生在设备针对区域和日历进行特定设置时发生。我可以重现的场景之一是:

  • 地区:印度尼西亚
  • 日历:Buddist

解决此问题的一种方法是将日历专门设置为您想要的格式。

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())
© www.soinside.com 2019 - 2024. All rights reserved.