Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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