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
Christian Dangaard
3,378 Points"You didn't add the 'incrementBy' method to the class 'RoundButton'
It seems straight forward, I always copy the code in XCode to test it and it seems to check out when I test the subclass RoundButton. But I am getting a "You didn't add 'incrementBy' method to the class 'RoundButton' or click Preview for compile errors." when I submit my response. The preview does not generate any errors either. Here's my response to the first part:
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) {
super.incrementBy(points)
}
}
I could be missing something simple, or just need coffee? Thank you in advanced.
1 Answer
Marina Alenskaja
9,320 PointsHi! You are missing a simple _ This is due to the fact that you specify a default value in the method. So when using the method, the parentheses in it don't need to be filled - it will just use the default value. I hope that makes sense. Like this:
override func incrementBy(_ points: Double = 7.0) {
super.incrementBy(points)
}
Christian Dangaard
3,378 PointsChristian Dangaard
3,378 PointsHellooo! Thank you so much! As I was following the instructor during the video (https://teamtreehouse.com/library/objectoriented-swift/inheritance/overriding-methods) my Xcode generated a warning "Extraneous '_' in parameter: 'points' has no keyword argument name", which is why I removed it from my notes. I think I understand why it's needed, as per: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html under the heading "Omitting External Parameter Names".
"If you do not want to use an external name for the second or subsequent parameters of a function, write an underscore (_) instead of an explicit external name for that parameter."
But I don't see how or where "points" having an explicit external name,. The same goes for the video example, where the default value was set for "percentage". I'll rewatch the video again and see he mentioned flagging/noting the warning. Thank you again.