I decided to continue my remaining project with Swift-language.When I added the custom class (.swift class which sub class to UIViewcontroller) in my storyboard viewcontroller and loaded the project the app crash suddenly with following error:
fatal error: use of unimplemented initializer 'init(coder:)' for class
This is a code :
import UIKit
class TestViewController: UIViewController {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Custom initialization
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
Please suggest something,
Issue
This is caused by the absence of the initializer
init?(coder aDecoder: NSCoder)
on the targetUIViewController
. That method is required because instantiating aUIViewController
from aUIStoryboard
calls it.To see how we initialize a
UIViewController
from aUIStoryboard
, please take a look hereWhy 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 targetUIViewController
2. Second method
Removing
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
on your targetUIViewController
will inherit all of the required initializers from the superclass as Dave Wood pointed on his answer belowFor those needing the code in Swift:
[Edit] This was for an older version of Swift. Possibly doesn't work anymore.
I had this problem in a programmatic collectionView cell and even though the op is asking about a vc I still landed on this question when searching for an answer. For me the issue was I did have
implemented so the top answer didn't work. What I didn't have in the cell was the initializer:
Once I added it the error went away
Another option besides @3r1d's is to instead remove the following init method from your class:
Including that init method, prevents the sub class from inheriting the
init(coder aDecoder: NSCoder!)
from its super class. By not including it, your class will inherit both.Note: See WWDC 2014 Session 403 "Intermediate Swift" at about the 33:50 mark for more details.
Rather than adding some methods for making internal mechanism work fine, i would go with defining my attributes as @lazy and initialise them right in the class scope.
For people having the same issue with swift
UICollectionViewCells
, add the code that @3r1d suggested to your customUICollectionViewCell
class and not to the View Controller: