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

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)
}

}