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)
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.
source share