Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This coder requires that replaced objects be returned from initWithCoder:

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.

like image 583
hemraj shaqawal Avatar asked Dec 19 '22 00:12

hemraj shaqawal


1 Answers

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()
    }
}
like image 118
iuriimoz Avatar answered Dec 24 '22 02:12

iuriimoz