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

designated initializer task

The task is asking to figure out why the designated initializer isn't working and I tried re working the last part of the initializer but I still can't get it to work. Any ideas? Thank you!

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
    super.init(width: Double, height: Double)
  }  
}

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

You are basically on the right track. You have to call super.init to set the initial values for width and height. However, you have to pass the actual values when calling the initializer. You passing the type "Double" instead of the width/height value, which is an error that should be highlighted in XCode.

Therefore

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

should be

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

In context, you can see that you set the corner radius in the initializer of RoundButton, and then pass on width and height to the designated initializer of the Button superclass.

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

Hope that helps!

That helps a lot, thank you!