I am overriding a UITableViewController
in swift, in which I have two required variables which are initialized by using a weak
reference of self
since these are used to implement UITableViewDataSource
protocol and need self
reference to use its tableView
property
class VideosListViewController: UITableViewController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.datasourceOfflineVideos = ASDataSource(tableViewController: self)
self.datasourceOnlineVideos = ASDataSource(tableViewController: self)
}
// MARK: - Variables
var datasourceOnlineVideos:ASDataSource
var datasourceOfflineVideos:ASDataSource
}
Now the problem is as it is giving me error Property not initialized at super.init call
which is expected as explained here: Error in Swift class: Property not initialized at super.init call.
Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error
Safety check 1 A designated initializer must ensure that all of the “properties introduced by its class are initialized before it delegates up to a superclass initializer.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
So my question is:
If an instance variable in swift class needs self
reference to be initialized like ASDataSource
type variables here, how can I do that??
Since swift doesn't allow me call the super.init()
before initiazing all the instance variables And also doesn't allow me to user self
within the initializer in the init()
before calling super.init()
Currently I am using optional variable(s) to resolve the issue. But for learning purpose I want to know how to do that. Thanks in advance :)