从Xcode 11.1升级到Xcode 11.2后,我的应用崩溃了:

***由于未捕获异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'无法实例化名为_UITextLayoutView的类,因为没有找到名为_UITextLayoutView的类;类需要在源代码中定义或从库中链接(确保类是正确目标的一部分)'

为什么会这样?我怎样才能防止这种崩溃?


当前回答

更新的解决方案: 更新到Xcode 11.2.1。对我来说,它可以在iOS 11、12或13设备上运行。

参考苹果的文档 此更新修复了一个可能导致使用UITextView的应用程序崩溃的关键问题。

旧的解决方案: 从https://developer.apple.com/download/more/下载Xcode 11.1 从11.2切换回11.1修复了崩溃。

而且,对我来说,即使我使用的是Xcode 11.2,当我将iPhone升级到13.2时,崩溃问题也得到了解决。

其他回答

你可以从苹果开发者网站下载最新的Xcode测试版(11.2.1 GM)。

这里是直接链接

一个更快的解决方法:

///Substitute class for _UITextLayoutView bug
class FixedTextView: UITextView {
    required init?(coder: NSCoder) {
        if #available(iOS 13.2, *) {
            super.init(coder: coder)
        }
        else {
            let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 44*3))
            super.init(frame: rect, textContainer: nil)
        }
    }
}

将此代码添加到某个地方,然后将所有故事板实例替换为FixedTextView。

注意:你将失去在故事板中创建的所有属性。这可能会产生严重的影响(例如委托设置、大小等)。

改进@garafajon的回答。对我来说,这在大多数情况下都是可行的。

///Substitute class for _UITextLayoutView bug
class FixedTextView: UITextView {
    required init?(coder: NSCoder) {
        if #available(iOS 13.2, *) {
            super.init(coder: coder)
        }
        else {
            super.init(frame: .zero, textContainer: nil)
            self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.contentMode = .scaleToFill

            self.isScrollEnabled = false   // causes expanding height

            // Auto Layout
            self.translatesAutoresizingMaskIntoConstraints = false
            self.font = UIFont(name: "HelveticaNeue", size: 18)
        }
    }
}

这是Xcode 11.2的一个bug。子类textview在所有没有安装新iOS版本(13.2)的设备上崩溃。您可能最好不要用那个版本构建一个发行版。

你现在可以:

把Xcode降级到11.1或者 把你的设备升级到iOS 13.2

我已经将khan的Obj-C解决方案改编为Swift:

import UIKit

@objc
class UITextViewWorkaround : NSObject {

    static func executeWorkaround() {
        if #available(iOS 13.2, *) {
        } else {
            let className = "_UITextLayoutView"
            let theClass = objc_getClass(className)
            if theClass == nil {
                let classPair: AnyClass? = objc_allocateClassPair(UIView.self, className, 0)
                objc_registerClassPair(classPair!)
            }
        }
    }

}

在AppDelegate中的didFinishLaunchingWithOptions末尾调用它。

谢谢@Aftab !