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
Arsene Lavaux
4,342 PointsError in overriding swift function
This code to answer the method overriding challenge in swift gives me an error although I am calling the base class method using "super". Any ideas why? Thanks!
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)
}
}
5 Answers
jcorum
71,830 PointsArsene, when I put your code in an Xcode playground it runs without error.
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)
}
}
let rb = RoundButton(width: 5, height: 12)
rb.cornerRadius //5
rb.width //5
rb.height //12
rb.incrementBy(5)
rb.width //10
rb.height //17
Just to test it I created a RoundButton object, displayed its properties, and then incremented it by 5.
As it happens, I got the same result with the overridden method commented out.
Arsene Lavaux
4,342 PointsApologize for poor formating, it's been a while since I last checked this forum. Hope someone can properly edit it.
Thanks!
jcorum
71,830 PointsP.S., to put your code in correctly you type 3 accent marks (on a mac keyboard the same key as the tilde) followed by the type of code (here swift), then a blank line, then the code, then 3 accent marks on the next line and then another blank line.
jcorum
71,830 PointsArsene, the key you use is the so-called accent grave `, not the accent acute '
Arsene Lavaux
4,342 PointsThanks @jcorum, you are probably right. And well versed in French accents! Bravo! We should connect on LinkedIn if you are open to it. I am always looking to connect with digital leaders, every day :) Synergies always come up along the way...
Arsene Lavaux
4,342 PointsArsene Lavaux
4,342 PointsThanks @jcorum: It does work for me too. Here is the example when I don't pass any argument to the function (which passes the 7 default value). All works! So there must be some sort of bug with this challenge? Anyone else experienced same issue?
Here is code:
'''swift
// Challenge Team Treehouse Override Method
import UIKit
class Button { var width: Double var height: Double
}
class RoundButton: Button { var cornerRadius: Double = 5.0
}
let rb = RoundButton(width: 5, height: 12) rb.cornerRadius //5 rb.width //5 rb.height //12 rb.incrementBy() rb.width //12 rb.height //19
'''