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

Not able to call convenience init

I am getting an error in designated initializer "Expected 'get', 'set', 'willSet' or 'didSet' keyword to start an accessor definition " and also not able to call convenience init.

I am not able to instantiate of Square using the convenience initializer .

Here is my code:

import UIKit
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 = 0.0
    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)
        } 
    }
}
var pol = Square(sides: 4, name: square)  

Please help me out. Thanks in advance

1 Answer

Nathan F.
Nathan F.
30,773 Points

Your problem is you forgot a closing curly brace, so your computed property, area, includes the init declarations inside its own curly braces. Add a curly brace where I marked it in a comment.

class Square : Shape {
    var sideLength: Double = 0.0
    var area: Double {
        get{
           return sideLength * sideLength
        }
        set{
            sideLength = sqrt(newValue)
        }
// ---> HERE 
        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)
        } 
    }
}

Thank you so much Nathan Fulkerson for helping to find out my silly mistake :D