After upgrading to Xcode 11.2 from Xcode 11.1, app

2020-01-24 10:11发布

After upgrading to Xcode 11.2 from Xcode 11.1 my app crashes:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named _UITextLayoutView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'

Why is this happening? How can I prevent this crash?

15条回答
在下西门庆
2楼-- · 2020-01-24 11:02

Congratulation

The New version of Xcode (11.2.1) is available now which is the best way to get rid off this issue.

Workarounds

@Mojtaba Hosseini the solution I proposed was from the help and the participation from my side to my fellow developers over StackOverflow. You, me and all the rest of the developer here already know that when the new version is announced by Apple, this issue will be gone.

But Beside Everything

The solution aforementioned was definitely accepted by Apple Review as there is no private API involved at all. This approach is very similar to the creating property like

@interface UITextView (Layout)

Or

UITextView+Layout.h

So when you are creating property you are directly using APPLE Private Components and re-moduling them as per you depends or requirement.

The Simple Example is AMFNetworking classes

- (void)setImageWithURL:(NSURL *)url {
    [self setImageWithURL:url placeholderImage:nil];
}

Hope I am done with the Allegation

The answer below was just some help from my side to enable developer to continue developing as you we initially proposed developer to roll back Xcode. This was a bad practice to download 8 GB Xcode again since we all know that the new version of Xcode will be released soon.

While it is fixed in Xcode 11.2.1, I got one solution for Xcode 11.2 by which you can get rid off this crash:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named _UITextLayoutView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'

SOLUTION

Go to the Build Setting search for "DEAD_CODE_STRIPPING" and set it to NO

DEAD_CODE_STRIPPING = NO

Then

create files UITextViewWorkaround

UITextViewWorkaround.h

    #import <Foundation/Foundation.h>


    @interface UITextViewWorkaround : NSObject
    + (void)executeWorkaround; 
@end

UITextViewWorkaround.m

#import "UITextViewWorkaround.h"
#import  <objc/runtime.h>



    @implementation UITextViewWorkaround

    + (void)executeWorkaround {
        if (@available(iOS 13.2, *)) {
        }
        else {
            const char *className = "_UITextLayoutView";
            Class cls = objc_getClass(className);
            if (cls == nil) {
                cls = objc_allocateClassPair([UIView class], className, 0);
                objc_registerClassPair(cls);
    #if DEBUG
                printf("added %s dynamically\n", className);
    #endif
            }
        }
    }

    @end

execute it in the app delegate

#import "UITextViewWorkaround.h"

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.

            [UITextViewWorkaround executeWorkaround];
    return yes;
    }

Compile the code and you will have a running app :)

查看更多
做自己的国王
3楼-- · 2020-01-24 11:02

I've adapted khan's Obj-C solution to 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!)
            }
        }
    }

}

Call it at the end of didFinishLaunchingWithOptions in AppDelegate.

Thanks @Aftab!

查看更多
4楼-- · 2020-01-24 11:03

11.2.1 GM seed resolves this issue

(and it can be used to publish to App Store)

Go to https://developer.apple.com/download/. Download Xcode 11.2.1 GM seed

Release notes confirm it fixes this error:

enter image description here

查看更多
一夜七次
5楼-- · 2020-01-24 11:03

You can go to download the last Xcode beta release (11.2.1 GM) from Apple developer website.

Here the direct link

Xcode 11.2.1 GM seed

查看更多
萌系小妹纸
6楼-- · 2020-01-24 11:07

Update: Fixed!

查看更多
我只想做你的唯一
7楼-- · 2020-01-24 11:09

As a "quick" fix you can add the UITextView directly from the code and not via IB. At least it worked for me. Though from my point of view it's better to rollback to previous Xcode/wait for the new one.

查看更多
登录 后发表回答