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 Overriding Methods

Van Sanders
Van Sanders
16,087 Points

Why wont this code pass?

When I set a default value for the roundButton classes incrementBy() method, I get a strange error mentioning that I didn't define a new 'incrementBy()' method (which is clearly not true).

When I omit the default value, it then asks for me to set one... What am I doing wrong?

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

2 Answers

Richard Lu
Richard Lu
20,185 Points

Hey Van,

Everything in your code looks right, but since the overridden method has an optional parameter, you must place an underscore as the parameters external name.

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

Hope I've helped. Happy Coding! :)

Richard Lu
Richard Lu
20,185 Points

Hey Van,

I notice that for Swift 1.2, this problem isn't occurring. Have you updated to XCode 7 with Swift 2.0?

Van Sanders
Van Sanders
16,087 Points

Yeah, sorry I was using this forum to help me with this issue, but the same issue is ocurring for me in Xcode 7 with Swift 2.1 - I got the two mixed up in my head.

Your underscore solution did indeed work for my treehouse issue. Thank you!