首页 > 解决方案 > 为什么点击键盘时iOS应用程序崩溃?

问题描述

点击键盘时我的应用程序崩溃,我选择了文本字段,键盘正确打开,但是一旦点击键盘(任何键)就会发生崩溃,并给出下面的日志。

我从 Xcode 13 RC 在带有 iOS 15 RC 的 iPhone 8 上运行该应用程序。在任何其他装有 iOS 14(无论版本)的设备上都可以正常工作。

调试控制台错误

2021-09-14 14:11:12.860707+0100 BonnetDriverDev[3595:649035] -[__NSArray0 stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x1f5c13500
2021-09-14 14:11:12.864293+0100 BonnetDriverDev[3595:649035] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x1f5c13500'
*** First throw call stack:
(0x180cddcac 0x197d0a748 0x180dad6d0 0x180c77edc 0x180c7714c 0x1830f752c 0x1834a2360 0x102f0e1c8 0x102f0fb1c 0x1830fad74 0x1835812a0 0x102f0e1c8 0x102f0fb1c 0x183019fc4 0x183019e28 0x183018c98 0x183496874 0x102f0e1c8 0x102f0fb1c 0x18351e144 0x183e49624 0x183e556b8 0x183e55180 0x183e48c3c 0x183e47db4 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e56594 0x183e47e20 0x183c81fac 0x183be8384 0x183c8afd4 0x183c8b0e4 0x183523ae0 0x1833dad2c 0x1823cc98c 0x180cfe220 0x180d0e248 0x180c515e8 0x180c56a18 0x180c69d8c 0x19ad2a9a0 0x18349e420 0x183231e18 0x100e585a4 0x101f0c190)
libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x1f5c13500'
terminating with uncaught exception of type NSException

我没有使用stringByTrimmingCharactersInSet函数,所以我猜是系统调用。我使用的库是可可豆荚。

您可以在苹果论坛上找到有关此问题的完整崩溃日志: https ://developer.apple.com/forums/thread/689868

调试导航器

图片中最后一个任务中的警告说:

Thread 1: "-[__NSArray0 stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x1fa3a3500"

调试导航器

初始化代码和视图控制器

这是一个很大的代码,但我减少到这个只是为了测试文本字段。

从应用程序委托初始化控制器

导入 UIKit

@UIApplicationMain 类 AppDelegate: UIResponder, UIApplicationDelegate {

public var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let vc = TestViewController(nibName: nil, bundle: nil)
  
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()
    
    return true
}... }

查看控制器内容

import UIKit

    class TestViewController: UIViewController {
        
        private var textField: UITextField {
            var v = UITextField()
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.setup()
        }
        
        private func setup() {
            self.view.addSubview(textField)
            
            NSLayoutConstraint.activate([
                self.textField.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
                self.textField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
                self.textField.heightAnchor.constraint(equalToConstant: 50),
                self.textField.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.8)
            ])
        }
    }

Pod:Firebase Auth、RemoteConfig、Analytics、GoogleSignIn、Stripe、Intercom、lottie

标签: iosswiftkeyboardbetaxcode13

解决方案


最后我找到了一个解决方案,但我很确定这太复杂了。

我的应用程序是使用 Xcode 10 生成的,因此我决定使用 Xcode 13 创建一个新项目,并将所有文件夹和配置移动到这个新项目中,这解决了我的错误和我遇到的任何其他问题。

如果有人有更好的解决方案,请分享。


推荐阅读