Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Inheritance Convenience Initializers

Jeroen de Vrind
Jeroen de Vrind
29,772 Points

Does the designated initializer need to call it's super designated initializer to set the variables width and height?

Quiz question is:

Is the following a valid convenience initializer? If not, then what is missing from it's definition?

 class RoundButton: Button {
  var cornerRadius: Double = 5.0

  init(width:Double,height:Double,cornerRadius:Double){
    self.cornerRadius = cornerRadius
  }  

  convenience init(length:Double) {
     self.init(width:length, height:length, cornerRadius: self.cornerRadius)
  }
}

2 Answers

Stephen McMillan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Stephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 Points

Your right - You do need a super.init in the designated initializer but that's not what the question is asking. Its asking whats wrong with the convenience initializer. Bit of a trick question if you ask me :P

Jeroen de Vrind
Jeroen de Vrind
29,772 Points

Yes that's true, but still: where are width and height set? You pass them in as a parameter, but you don't call the superclass's initializer so the properties aren't set. I think there need to be a super.init(width: width, height: height) in the designated initializer after the statement self.cornerRadius = cornerRadius