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:)
?