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 Inheritance and Initializers

Adam Bridges
Adam Bridges
1,904 Points

RoundButton designated initializer

What's wrong with my designated initializer for RoundButton? Here's my code:

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

3 Answers

Jens Hagfeldt
Jens Hagfeldt
16,548 Points

Hi Adam.

Remember when subclassing another class you always need to also make a call to the base classes initializer to initialize the base classes properties (in this case: width and height). Also remember that this always should be done after initializing the subclasses own properties (in this case: cornerRadius). The call to the base classes initializer is done by adding the line: super.init(width: width, height: height) like below.

class RoundButton: Button {
  var cornerRadius: Double = 5.0

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

I hope this explanation of mine made sense for you...

Happy coding! / Jens

Jens Hagfeldt
Jens Hagfeldt
16,548 Points

I have corrected the typo now... sorry again.

Hi Jens, thanks to have fixed it, I just don't understand yet why this is correct:

super.int(width: width, height: width)

and not this:

super.int(width: width, height: height)

I don't get why height: width vs height: height

thanks again

Jens Hagfeldt
Jens Hagfeldt
16,548 Points

Sorry for the typo. It should be " height: height "

oh ok, thanks. But even with the typo the code passed. Any explanation for it too? thanks for your time :)