An error in the Swift class: the property is not initialized when super.init is called - how to initialize properties that require the use of self in the initializer parameter

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) } // MARK: - Variables var datasourceOnlineVideos:ASDataSource var datasourceOfflineVideos:ASDataSource } 

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

+3
initialization properties ios swift compiler-errors
Nov 19 '14 at 23:11
source share
1 answer

You just need to invert the order of super.init / properties in your initializer:

  required init(coder aDecoder: NSCoder) { self.datasourceOfflineVideos = ASDataSource(tableViewController: self) self.datasourceOnlineVideos = ASDataSource(tableViewController: self) super.init(coder: aDecoder) } 

instance properties in the first place, then you can call the superclass initializer. But this is not possible in your case, because you are referring to self .

The workaround in this case is to make the properties of implicitly expanded options:

 var datasourceOnlineVideos:ASDataSource! var datasourceOfflineVideos:ASDataSource! 

Since the optional parameters do not need to be initialized, you can safely initialize them after calling the super.init method. Being implicitly deployed, you use them like any other optional property.

This template is used by Apple in several classes, including the UIViewController : when you add an exit from IB, the component property is always declared implicitly expanded. This is because the control itself is not created in the initializer, but at a later stage.

+9
Nov 19 '14 at 23:14
source share



All Articles