Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Value vs Reference Types Final Exam - Solution

Im 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
Chris Shaw
26,650 Points

Hi 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!

Good eye! I seriously looked through the code so many times, but couldn't find that mistake. Thanks for taking the time!