Fatal error: use of unimplemented initializer ‘init(coder:)’ for class

Issue

This is caused by the absence of the initializer init?(coder aDecoder: NSCoder) on the target UIViewController. That method is required because instantiating a UIViewController from a UIStoryboard calls it.

To see how we initialize a UIViewController from a UIStoryboard, please take a look here

Why is this not a problem with Objective-C?

Because Objective-C automatically inherits all the required UIViewController initializers.

Why doesn’t Swift automatically inherit the initializers?

Swift by default does not inherit the initializers due to safety. But it will inherit all the initializers from the superclass if all the properties have a value (or optional) and the subclass has not defined any designated initializers.


Solution

1. First method

Manually implementing init?(coder aDecoder: NSCoder) on the target UIViewController

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

2. Second method

Removing init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) on your target UIViewController will inherit all of the required initializers from the superclass as Dave Wood pointed on his answer below


Leave a Comment