Swift Overload init ()

i will overload the init method in Swift, how can I implement this?

here is my code that doesn't work

code removed

Edit:

So this will work fine

override init() { super.init(); } init(title:String?) { super.init(); self.title = title } convenience init(title:String?, imageName:String?) { self.init(title:title) self.imageName = imageName } convenience init(title:String?, imageName:String?, contentKey:String?) { self.init(title:title, imageName:imageName) self.contentKey = contentKey } 
+5
source share
1 answer

I connected the sample to the playground, here is how I got it to work:

 class Y { } class X : Y { var title: String? var imageName: String? override init() { } init(aTitle:String?) { super.init() self.title = aTitle } convenience init(aTitle:String?, aImageName:String?) { self.init(aTitle: aTitle) self.imageName = aImageName } } 
  • init(aTitle:String?, aImageName:String?) cannot call init(aTitle:) and still be a designated initializer, it must be a convenience initializer.
  • self.init must be before anything else in init(aTitle:String?, aImageName:String?) .
  • The initializer must be passed the parameter name self.init(aTitle) must be self.init(aTitle: aTitle) .

As a side note, I removed the unnecessary semicolons and put super.init() first for style reasons.

Hope this helps.


UPDATE

To follow Apple's recommendations, there should be only one assigned, the rest should be convenience initializers. For example, if you decide that init(aTitle:String?) Should be the designated initializer, then the code should look like this:

 convenience override init() { self.init(aTitle: nil) // A convenience initializer calls the designated initializer. } init(aTitle:String?) { super.init() // Only the designated initializer calls super.init. self.title = aTitle } convenience init(aTitle:String?, aImageName:String?) { self.init(aTitle: aTitle) // A convenience initializer calls the designated initializer. self.imageName = aImageName } 

Sometimes you may need to have more than one assigned initializer, such as a UIView , but this should be the exception, not the rule.


UPDATE 2

Classes must have one designated initializer. The convenience initializer (in the end) will invoke the designated initializer.

Initialization

A class can have multiple initializers. This happens when the initialization data can take various forms or when some initializers provide default values ​​as a convenience. In this case, one of the initialization methods is called the designated initializer , which fully fulfills the initialization parameters.

Multiple Initializers

Assigned Initializer

A class initializer that accepts a full set of initialization parameters is usually a designated initializer. The designated initializer of the subclass must invoke the designated initializer of its superclass by sending a super message. Convenience initializers (or secondary), which may include init-do not super. Instead, they call (via a message to themselves) the initializer in a series with the following most parameters, providing a default value for the parameter not passed to it. The final initializer in this series is the designated initializer.

+11
source

Source: https://habr.com/ru/post/1204163/


All Articles