问题
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.
回答1:
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:
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()
}
}
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/49654052/this-coder-requires-that-replaced-objects-be-returned-from-initwithcoder