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 trialAdam Bridges
1,904 PointsRoundButton 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
16,548 PointsHi 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
macche
3,989 PointsHi 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
16,548 PointsSorry for the typo. It should be " height: height "
macche
3,989 Pointsoh ok, thanks. But even with the typo the code passed. Any explanation for it too? thanks for your time :)
Jens Hagfeldt
16,548 PointsJens Hagfeldt
16,548 PointsI have corrected the typo now... sorry again.