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

Having trouble with this question. Any help would be great!

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

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Paul.

Because roundButton inherits from Button, you also need to include a super.init for the inherited values.

class RoundButton: Button {
  var cornerRadius: Double = 5.0

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

Because you are using properties from the Super Class, they must also be initialized, by using super.init. Amit goes over this in this video starting just after the 3 minute mark.

Keep Coding! :)