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

I need your help

Could anyone help me to find a solution to this challenge ?

Challenge Task 1 of 1

Once again we are working with the RoundButton class and started to add a designated initializer to it. However, there's something wrong with it. Can you figure out how to fix it and make it work?

Button.swift
class Button {
  var width: Double
  var height: Double

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

  func scaleBy(points: Double){
    width += points
    height += points
  }
}

class RoundButton: Button {
  var cornerRadius: Double = 5.0

  init(width:Double,height:Double,cornerRadius:Double){
    self.cornerRadius = cornerRadius
  }  
}
Chokdee Srisuk
Chokdee Srisuk
19,384 Points

In designated initializer of class RoundButton, you need to add designated of super class to it.

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

So class RoundButton would be

class RoundButton: Button { var cornerRadius: Double = 5.0

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

4 Answers

Chokdee Srisuk
Chokdee Srisuk
19,384 Points

class RoundButton: Button {

var cornerRadius: Double = 5.0


  init(width:Double,height:Double,cornerRadius:Double){

    self.cornerRadius = cornerRadius

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

  }

}

thank you but could you write the correct answer again ?

Thank you

Dave Pinchoff
Dave Pinchoff
4,440 Points

I don't know if this is an appropriate space to ask this question, but it tags onto what has already been said, so thanks for the help in advance:

Why is it that in the super.init call, we write (width: width, height: height)?

I was expecting to write (width: Double, height: Double).

Kenneth Giorno
Kenneth Giorno
4,043 Points

because we've already passed in the width and height variables as doubles in the start of the init method. we then re-use these values in the super.init call

Dave Pinchoff
Dave Pinchoff
4,440 Points

Kenneth -

ahh... thanks - i see that now.

happy coding!