简体   繁体   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)

My app has suddenly started crashing when keyboard type is numberPad or phonePad. 当键盘类型为numberPad或phonePad时,我的应用突然崩溃。 The crash happen in simulator and device both. 崩溃发生在模拟器和设备上。 I am setting keyboard type by below code. 我通过下面的代码设置键盘类型。

self.textFieldMobileNumber.keyboardType = .numberPad

Default keyboard just works fine. 默认键盘工作正常。 The crash log is blow 崩溃日志被打击

*** 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

I have searched all over the internet and tried many solution from my side but the issue did not fix. 我在互联网上进行了搜索,并尝试了许多解决方案,但问题并未解决。

Edit 编辑

When I set default keyboard. 当我设置默认键盘时。 It shows (null) for number keyboard, switching to that does not crash but when set .numberPad keyboard it starts crashing. 它显示数字键盘为(null),切换到该按钮不会崩溃,但是在设置.numberPad键盘时它开始崩溃。

Check the image below for the reference 检查下面的图像以供参考

在此处输入图片说明

Finally I found the issue. 终于我找到了问题。 I had overridden a method of NumberFormatter which caused the crash while showing numberPad keyboard. 我重写了NumberFormatter的方法,该方法在显示numberPad键盘时导致崩溃。

Below is the method that I override which should not have been done, or done in some other way. 下面是我应该重写的方法,这些方法不应该完成,或应以其他方式完成。

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

Returning nil caused the crash. 返回nil导致崩溃。 Also if you return something else rather then the obj it will print the same in keyboard keys. 同样,如果您返回其他内容而不是obj ,它将在键盘键中打印相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM