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

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