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

Kenton Raiford
Kenton Raiford
5,101 Points

getting error in convenience init "Use of unresolved identifier 'sideLength'

I am not sure why I keep getting this error. Anyone know what is wrong?

// Create a base class called Shape which will have 2 properties: sides and name

class Shape { let sides: Int let name: String

init(sides: Int, name: String) {
    self.sides = sides
    self.name = name
}

}

// Create a subclass called Square, it will also have 2 properties called: sideLength and area. // The area property will be a computed property with getter and setter methods.

class Square: Shape { var sideLength: Double var area: Double { get { return sideLength * sideLength } set { sideLength = sqrt(newValue) } }

// Add a designated initializer to Square which accept all three

init(name: String, sides: Int, sideLength: Double) {
    self.sideLength = sideLength
    super.init(sides: sides, name: name)
}
// Add a convenience initializer to Square that will accept only the sideLength and provide default values. 4 for sides and “Square” for name

convenience init (sideLength: Double) {
    self.init(name: "Square", sides: 4, sideLength:sidelength)
}

}

let square = Square(sideLength: 20) square.name square.sides square.sideLength square.area square.area = 100 square.sideLength

Kenton Raiford
Kenton Raiford
5,101 Points

I figured out it was because I didn't capitalize 'sidelength' in the end.

convenience init (sideLength: Double) { self.init(name: "Square", sides: 4, sideLength: sidelength <----) }


when I corrected it, it worked.

convenience init (sideLength: Double) {
    self.init(name: "Square", sides: 4, sideLength:sideLength)
}

}