从Xcode 11.1升级到Xcode 11.2后,我的应用崩溃了:
***由于未捕获异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'无法实例化名为_UITextLayoutView的类,因为没有找到名为_UITextLayoutView的类;类需要在源代码中定义或从库中链接(确保类是正确目标的一部分)'
为什么会这样?我怎样才能防止这种崩溃?
从Xcode 11.1升级到Xcode 11.2后,我的应用崩溃了:
***由于未捕获异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'无法实例化名为_UITextLayoutView的类,因为没有找到名为_UITextLayoutView的类;类需要在源代码中定义或从库中链接(确保类是正确目标的一部分)'
为什么会这样?我怎样才能防止这种崩溃?
当前回答
我使用了一个成功的变通方法,但很痛苦。以下是我遵循的流程:
在文本编辑器中打开XIB 找到违规的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>
注意它的id(在我的例子中:id="782-j1-88c") 如上面的答案所述,重写类并重新创建选项(我的是Objective-C,抱歉):
@implementation FixedTextView
- (id) initWithCoder:(NSCoder*)coder
{
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){13,2,0}])
self = [super initWithCoder:coder];
else {
self = [super initWithFrame:CGRectMake(16, 3, 343, 605)];
self.editable = YES;
self.selectable = YES;
self.insetsLayoutMarginsFromSafeArea = YES;
self.clipsToBounds = YES;
self.clearsContextBeforeDrawing = YES;
self.autoresizesSubviews = YES;
self.contentMode = UIViewContentModeScaleToFill;
self.scrollEnabled = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.translatesAutoresizingMaskIntoConstraints = NO;
self.font = [UIFont fontWithName:@"Menlo-Regular" size:12.0];
}
return self;
}
注意包含文本视图id的约束,并根据视图或视图控制器中的其他元素id重新创建这些约束。在我的例子中:
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self xibSetup];
[self initView];
/*
<constraint firstItem="75C-lt-YtE" firstAttribute="top" secondItem="782-j1-88c" secondAttribute="bottom" constant="8" symbolic="YES" id="8SH-5l-FAs"/>
<constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="Mve-aZ-HCe"/>
<constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="75C-lt-YtE" secondAttribute="leading" id="dPG-u3-cCi"/>
<constraint firstItem="782-j1-88c" firstAttribute="trailing" secondItem="iN0-l3-epB" secondAttribute="trailingMargin" id="sjT-0Q-hNj"/>
<constraint firstItem="782-j1-88c" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" id="vic-vZ-osR"/>
*/
[self.command.topAnchor constraintEqualToAnchor:self.console.bottomAnchor constant:8].active = YES;
[self.console.leadingAnchor constraintEqualToAnchor:self.layoutMarginsGuide.leadingAnchor].active = YES;
[self.console.leadingAnchor constraintEqualToAnchor:self.command.leadingAnchor].active = YES;
[self.console.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
[self.console.topAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor].active = YES;
}
return self;
}
这样做解决了我的问题,而没有损失所需的功能。幸运的是,我只有一个UITextView要替换。否则,这就站不住脚了。
其他回答
我已经将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 !
一个更快的解决方法:
///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。
注意:你将失去在故事板中创建的所有属性。这可能会产生严重的影响(例如委托设置、大小等)。
我也有同样的问题,我只是把我的Xcode 11.2升级到11.2.1,它工作得很好。
升级后,我在iOs 13和iOs 12上测试了同样的功能,效果很好。
该问题已在Xcode 11.2.1中修复。
编辑:由于修复现在已经发布,你应该切换到Xcode版本并注释掉这个解决方案。Mojtaba Hosseini在他的答复中提到:
... 这最后两个灵活的变通方法使用了苹果私有API,将被苹果审查拒绝!
在苹果发布修复之前,这是一个继续开发和测试的好办法。
对于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”调用解决方案
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// This is the workaround for Xcode 11.2
UITextViewWorkaround.unique.executeWorkaround()
}
我使用了一个成功的变通方法,但很痛苦。以下是我遵循的流程:
在文本编辑器中打开XIB 找到违规的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>
注意它的id(在我的例子中:id="782-j1-88c") 如上面的答案所述,重写类并重新创建选项(我的是Objective-C,抱歉):
@implementation FixedTextView
- (id) initWithCoder:(NSCoder*)coder
{
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){13,2,0}])
self = [super initWithCoder:coder];
else {
self = [super initWithFrame:CGRectMake(16, 3, 343, 605)];
self.editable = YES;
self.selectable = YES;
self.insetsLayoutMarginsFromSafeArea = YES;
self.clipsToBounds = YES;
self.clearsContextBeforeDrawing = YES;
self.autoresizesSubviews = YES;
self.contentMode = UIViewContentModeScaleToFill;
self.scrollEnabled = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = YES;
self.translatesAutoresizingMaskIntoConstraints = NO;
self.font = [UIFont fontWithName:@"Menlo-Regular" size:12.0];
}
return self;
}
注意包含文本视图id的约束,并根据视图或视图控制器中的其他元素id重新创建这些约束。在我的例子中:
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self xibSetup];
[self initView];
/*
<constraint firstItem="75C-lt-YtE" firstAttribute="top" secondItem="782-j1-88c" secondAttribute="bottom" constant="8" symbolic="YES" id="8SH-5l-FAs"/>
<constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="Mve-aZ-HCe"/>
<constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="75C-lt-YtE" secondAttribute="leading" id="dPG-u3-cCi"/>
<constraint firstItem="782-j1-88c" firstAttribute="trailing" secondItem="iN0-l3-epB" secondAttribute="trailingMargin" id="sjT-0Q-hNj"/>
<constraint firstItem="782-j1-88c" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" id="vic-vZ-osR"/>
*/
[self.command.topAnchor constraintEqualToAnchor:self.console.bottomAnchor constant:8].active = YES;
[self.console.leadingAnchor constraintEqualToAnchor:self.layoutMarginsGuide.leadingAnchor].active = YES;
[self.console.leadingAnchor constraintEqualToAnchor:self.command.leadingAnchor].active = YES;
[self.console.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
[self.console.topAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor].active = YES;
}
return self;
}
这样做解决了我的问题,而没有损失所需的功能。幸运的是,我只有一个UITextView要替换。否则,这就站不住脚了。