5

My application is working fine with iOS 11.2 but in iOS 11.3 is going crash. i got exception

Terminating app due to uncaught exception 'NSGenericException', reason: 'This coder requires that replaced objects be returned from initWithCoder

I have one viewController with tableView and this tableView have 2 cells somehow this table view can't able to load a cell in cellForRowAtIndexPath Method.

LPDiscoverFeedCell *cell = (LPDiscoverFeedCell *)[tableView dequeueReusableCellWithIdentifier:checkPortrait];

this is exception point where i got this.

4 Answers 4

9

Xcode 10.2 updated compiler with new feature:

To reduce the size taken up by Swift metadata, convenience initializers defined in Swift now only allocate an object ahead of time if they’re calling a designated initializer defined in Objective-C. In most cases, this has no effect on your app, but if your convenience initializer is called from Objective-C and doesn’t in turn delegate via self.init to an initializer exposed to Objective-C, the initial allocation from alloc is released without any initializer being called. This can be problematic for users of the initializer that don’t expect any sort of object replacement to happen. One instance of this is with initWithCoder:: the implementation of NSKeyedUnarchiver may behave incorrectly if it calls into Swift implementations of initWithCoder: and the archived object graph contains cycles. To avoid this, ensure that convenience initializers that don’t support object replacement always delegate to initializers that are also exposed to Objective-C, either because they’re defined in Objective-C, or because they’re marked with @objc, or because they override initializers exposed to Objective-C, or because they satisfy requirements of an @objc protocol. (46823518)" https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes/swift_5_release_notes_for_xcode_10_2?language=objc

I had MyClass in Storyboard scene:

enter image description here

If MyClass has convenience init() which calls designated initializer, then it should be marked with @objc:

class MyClass: NSObject {
    override convenience init() {
        self.init(int: 42)
    }

    // Add @objc to stop crashes
    @objc init(int: Int) {
        super.init()
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That fixed it for me. :) Great answer. I was completely at a loss.
Great answer for anyone interesting in injecting Foundation types. In my case, I wanted to inject an custom object conforming to a protocol in the scenario you outlined and the above solution didn't work until I marked my protocol with @objc.
3

I started seeing this crash once i updated my xcode from 10.0 to 10.2.1

The issue was present in the "InputMask" library version 4.1.0 and the issue has been fixed in its version 4.1.1. I was using carthage, so, all I had to do was run carthage update command

For more information on the commit that fixed crash, here is the link:

NSGenericException: This coder requires that replaced objects be returned from initWithCoder

Comments

2

In my case, it works after turning off the "Prefers coder at runtime" in IB.

enter image description here

Comments

0

Did you register the LPDiscoverFeedCell? If not, Please try this,

Option 1

[self.tableView registerClass:[LPDiscoverFeedCell class] forCellReuseIdentifier:@"checkPortrait"];

Option 2

UINib *feedCell = [UINib nibWithNibName:@"LPDiscoverFeedCell" bundle:nil];
[self.tableView registerNib:feedCell forCellReuseIdentifier:@"checkPortrait"];

And see if this resolves the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.