簡體   English   中英

當鍵盤為numberPad,phonePad或decimalPad(iOS 12.2,Xcode 10.2)時,應用程序崩潰

[英]App crashes when keyboard is numberPad, phonePad or decimalPad (iOS 12.2, Xcode 10.2)

當鍵盤類型為numberPad或phonePad時,我的應用突然崩潰。 崩潰發生在模擬器和設備上。 我通過下面的代碼設置鍵盤類型。

self.textFieldMobileNumber.keyboardType = .numberPad

默認鍵盤工作正常。 崩潰日志被打擊

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM addObject:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000112d0f6fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x0000000112051ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000112c5dddc _CFThrowFormattedException + 194
    3   CoreFoundation                      0x0000000112c81601 -[__NSSetM addObject:] + 817
    4   UIKitCore                           0x000000011ca49f8f +[UIKeyboardImpl uniqueNumberPadInputModesFromInputModes:forKeyboardType:] + 1257
    5   UIKitCore                           0x000000011ca4998f -[UIKeyboardImpl desirableInputModesWithExtensions:] + 1446
    6   UIKitCore                           0x000000011ca4a0cf -[UIKeyboardImpl recomputeActiveInputModesWithExtensions:] + 54
    7   UIKitCore                           0x000000011ca3b158 -[UIKeyboardImpl setDelegate:force:] + 3343
    8   UIKitCore                           0x000000011ca8db1e -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1506
    9   UIKitCore                           0x000000011cc4d417 -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] + 81
    10  UIKitCore                           0x000000011cc4916a -[UIResponder becomeFirstResponder] + 865
    11  UIKitCore                           0x000000011d0d4fb8 -[UIView(Hierarchy) becomeFirstResponder] + 145
    12  UIKitCore                           0x000000011cf269ec -[UITextField becomeFirstResponder] + 237
    13  UIKitCore                           0x000000011cee67a5 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 208
    14  UIKitCore                           0x000000011ced3853 -[UITextSelectionInteraction oneFingerTap:] + 3851
    15  UIKitCore                           0x000000011c80714d -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    16  UIKitCore                           0x000000011c80fc69 _UIGestureRecognizerSendTargetActions + 109
    17  UIKitCore                           0x000000011c80d5ba _UIGestureRecognizerSendActions + 311
    18  UIKitCore                           0x000000011c80c897 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 966
    19  UIKitCore                           0x000000011c7fec4e _UIGestureEnvironmentUpdate + 2820
    20  UIKitCore                           0x000000011c7fe108 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 478
    21  UIKitCore                           0x000000011c7fde96 -[UIGestureEnvironment _updateForEvent:window:] + 200
    22  UIKitCore                           0x000000011cc595bc -[UIWindow sendEvent:] + 4057
    23  UIKitCore                           0x000000011cc37d16 -[UIApplication sendEvent:] + 356
    24  UIKitCore                           0x000000011cd08293 __dispatchPreprocessedEventFromEventQueue + 3232
    25  UIKitCore                           0x000000011cd0abb9 __handleEventQueueInternal + 5911
    26  CoreFoundation                      0x0000000112c76be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    27  CoreFoundation                      0x0000000112c76463 __CFRunLoopDoSources0 + 243
    28  CoreFoundation                      0x0000000112c70b1f __CFRunLoopRun + 1231
    29  CoreFoundation                      0x0000000112c70302 CFRunLoopRunSpecific + 626
    30  GraphicsServices                    0x00000001171ab2fe GSEventRunModal + 65
    31  UIKitCore                           0x000000011cc1dba2 UIApplicationMain + 140
    32  IMRide                              0x000000010b39beb8 main + 72
    33  libdyld.dylib                       0x000000011560a541 start + 1
    34  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我在互聯網上進行了搜索,並嘗試了許多解決方案,但問題並未解決。

編輯

當我設置默認鍵盤時。 它顯示數字鍵盤為(null),切換到該按鈕不會崩潰,但是在設置.numberPad鍵盤時它開始崩潰。

檢查下面的圖像以供參考

在此處輸入圖片說明

終於我找到了問題。 我重寫了NumberFormatter的方法,該方法在顯示numberPad鍵盤時導致崩潰。

下面是我應該重寫的方法,這些方法不應該完成,或應以其他方式完成。

extension NumberFormatter {
    open override func string(for obj: Any?) -> String? {
         if condition {
             return <some value based on condition>
         }
         return nil
    }
}

返回nil導致崩潰。 同樣,如果您返回其他內容而不是obj ,它將在鍵盤鍵中打印相同的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM