改进@garafajon的答案。对我来说,在大多数情况下都可以使用。
从Xcode 11.1升级到Xcode 11.2后,我的应用崩溃了:
***由于未捕获的异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'由于未找到名为_UITextLayoutView的类,因此无法实例化名为_UITextLayoutView的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'
为什么会这样?如何防止此崩溃?
您可以从Apple开发人员网站下载最新的XCode beta版本(11.2.1 GM)。
这是Xcode 11.2的错误。子类化的Textviews在未安装neweset iOS版本(13.2)的所有设备上崩溃。您最好不要使用该版本构建发行版。
您现在可以:
我使用了成功的解决方法,但这很痛苦。这是我遵循的过程:
TextView
。就我而言:<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="782-j1-88c" customClass="LCAnsiConsoleTextView">
<rect key="frame" x="16" y="20" width="343" height="589"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" name="Menlo-Regular" family="Menlo" pointSize="12"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
DEAD_CODE_STRIPPING = NO
在项目的构建设置中对其进行了修复。
虽然已在Xcode 11.2.1中修复,但我得到了Xcode 11.2的一种解决方案,通过它可以摆脱此崩溃:
***由于未捕获的异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'由于未找到名为_UITextLayoutView的类,因此无法实例化名为_UITextLayoutView的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'
解决方案
此问题已在Xcode 11.2.1中修复。对于Xcode 11.2,基于Aftab Muhammed Khan的想法,并在John Nimis的帮助下,我仅测试了以下代码。
无需在情节提要中进行任何更改!
编辑了我的AppDelegate.swift文件并添加了此类
//****************************************************************** // MARK: - Workaround for the Xcode 11.2 bug //****************************************************************** class UITextViewWorkaround: NSObject { // -------------------------------------------------------------------- // MARK: Singleton // -------------------------------------------------------------------- // make it a singleton static let unique = UITextViewWorkaround() // -------------------------------------------------------------------- // MARK: executeWorkaround() // -------------------------------------------------------------------- func executeWorkaround() { if #available(iOS 13.2, *) { NSLog("UITextViewWorkaround.unique.executeWorkaround(): we are on iOS 13.2+ no need for a workaround") } else { // name of the missing class stub let className = "_UITextLayoutView" // try to get the class var cls = objc_getClass(className) // check if class is available if cls == nil { // it's not available, so create a replacement and register it cls = objc_allocateClassPair(UIView.self, className, 0) objc_registerClassPair(cls as! AnyClass) #if DEBUG NSLog("UITextViewWorkaround.unique.executeWorkaround(): added \(className) dynamically") #endif } } } }
并且在“ didFinishLaunchingWithOptions”的委托调用中调用解决方法
我已将可汗的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!)
}
}
}
}
感谢Aftab!
更快的解决方法:
从https://developer.apple.com/download/more/下载了Xcode 11.1从11.2切换回11.1可以解决崩溃问题。
而且,即使对于Xcode 11.2,当我将iPhone升级到13.2时,这也解决了当机问题。
改进@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)
}
}
}
(并且可以用于发布到App Store)
转到https://developer.apple.com/download/。下载Xcode 11.2.1 GM种子
改进@garafajon的答案。对我来说,在大多数情况下都可以使用。
作为“快速”修复,您可以直接从代码中添加UITextView
,而无需通过IB。至少对我有用。尽管从我的角度来看,最好回滚到以前的Xcode /等待新的Xcode。
您可以从Apple开发人员网站下载最新的XCode beta版本(11.2.1 GM)。
希望此帮助😉