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

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
  }  
}

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

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!