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 trialBenjamin Gomola
1,761 PointsIm getting the error message use of 'self' in delegating initializer before self.init is called. But why?
This is the code I am using (identical to the one Amit has written in the final exam task:
class Shape {
let sides: Int
let name: String
init(sides: Int, name: String) {
self.sides = sides
self.name = name
}
}
class Square: Shape {
var sideLength: Double
var area: Double {
get {
return sideLength * sideLength
}
set {
sideLength = sqrt(newValue)
}
}
init(name: String, sides: Int, sideLength: Double) {
self.sideLength = sideLength
super.init(sides: sides, name: name)
}
convenience init(sidelength: Double) {
self.init(name: "Square", sides: 4, sideLength: sideLength) // the error comes up for this line
}
}
let square = Square(sidelength: 20)
square.name
2 Answers
Chris Shaw
26,676 PointsHi Benjamin,
You have a very minor typo which is causing the error, your parameter name is called sidelength
while the variable you're passing to the init
declaration is sideLength
, notice one has an lowercase L and the other is uppercase. Simply adjust the casing for one of the two and the error will disappear.
Happy coding!
Benjamin Gomola
1,761 PointsGood eye! I seriously looked through the code so many times, but couldn't find that mistake. Thanks for taking the time!