I override the UITableViewController in swift, in which I have two required variables that are initialized with a weak self reference, as they are used to implement the UITableViewDataSource protocol and need a self reference to use their tableView property
class VideosListViewController: UITableViewController { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.datasourceOfflineVideos = ASDataSource(tableViewController: self) self.datasourceOnlineVideos = ASDataSource(tableViewController: self) }
Now the problem is that it gives me the Property not initialized at super.init call , which is expected as described here: Error in the Swift class: the property was not initialized when super.init was called .
The Swifts compiler performs four useful security checks to ensure that two-phase initialization completed without errors.
Security Check 1 The designated initializer must ensure that all "properties introduced by its class are initialized before it delegates to the superclass initializer.
Excerpt from: Apple Inc. "Fast Programming Language." interactive books. https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
So my question is:
If the instance variable in the swift class needs to initialize the self reference, for example, as variables of the ASDataSource type, how to do this?
Since swift does not allow me to call super.init() before initializing all instance variables, it also does not allow the user self in the initializer in init() before calling super.init()
I am currently using optional variables to solve the problem. But for training, I want to know how to do it. Thanks in advance:)
initialization properties ios swift compiler-errors
iMemon Nov 19 '14 at 23:11 2014-11-19 23:11
source share