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

Code Challenge Object Oriented Swift; Inheritance

The prompt is too problem solve and figure out why the code will not work. I added an super.init into my initializer for the subclass. Not sure what is going on here.

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

4 Answers

Brendan,

Give this a try:

class Button {
  var width: Double
  var height: Double

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

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

class RoundButton: Button {
  var cornerRadius: Double = 5.0
  override func incrementBy(_ points: Double = 7.0){
    width += points
    height += points
  }
}

Thanks again!!

Anytime!

Eric Caine
Eric Caine
1,659 Points

Why does this work? It doesn't seem to be what the challenge is asking for...? (But it does work!)

Peter Do
Peter Do
12,294 Points

yeah I was trying the same as Brendan - I feel the same way Eric, I'm not sure that's what the question was asking for... but the code worked for the challenge?

I tried the code here for the challenge and it did not work. My initial stabs at it followed the same logic as Brendan's (almost the exact same code). Can anybody provide guidance on this one?