Reputation: 3875
Part 1
I am writing a Today Widget extension using NSExtensionPrincipalClass
instead of storyboard. I implemented the following required init
required init(coder aDecoder: NSCoder) {
tableView = UITableView()
cellIdentifier = "kCellIdentifier"
super.init(coder: aDecoder)
}
However, when I run the project I get the following error
use of unimplemented initializer 'init()'
So, by putting this additional code in the project, it will fix the problem but it doesn't seem right to initialize variables in multiple places
override init() {
tableView = UITableView()
cellIdentifier = "kCellIdentifier"
super.init()
}
Part 2
So I answered part 1 of my question below but I still don't know why the following solution wouldn't work?
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
cellIdentifier = "kCell"
tableView = UITableView()
super.init(nibName: nil, bundle: nil)
}
As per Swift's documentation
“If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.”
In UIViewController, init(nibName:bundle:)
is the only designated initialized so why am I not automatically inherits init()
and init(coder:)
?
Upvotes: 3
Views: 4491
Reputation: 3875
So here is the solution to the first part of my question after re-read swift's initializer chapter and look at UIViewController header file
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init() {
cellIdentifier = "kCellIdentifier"
tableView = UITableView()
super.init(nibName: nil, bundle: nil)
}
One of the key to this solution is init(nibName:bundle:)
which is a designated initializer which you must call whether you use storyboard or not.
Keys take away:
convenience init
will call init(nibName:bundle:)
for you using nil
as arguments. So if you are not inherited the default init() method then it is your responsibility to override it and call init(nibName:bundle:)
Upvotes: 4