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 Final Exam

I did the code for the final exam in my xcode playground, but I am getting errors with the convenience initializer and none of the values are showing up on the right side of my screen. Can anyone help me out?

Here is my code:

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(sideLength: Double) {
        self.sideLength = sideLength
        super.init(sides: sides, name: name)
    }

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


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

2 Answers

Hi Mia Pivirotto, You were pretty much right but I advise following a rule of thumb which is starting new lines for various things you do. Your code was pretty much right but when I spaced it out and put things on their own lines and what not (demonstrated in code below) I noticed an extra curly brace or so, I believe this was throwing an error. However here is your code just laid out differently:

import Foundation

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

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

Also one thing that would have thrown you an error would have been "sqrt", you need to make sure if you work in xcode playground you import Foundation at the top for this to work. I'm no expert in Swift but taking on good practices like spacing and laying code out in a readable manner (so curly braces and brackets are on their own lines) although its not essential, it can help you spot tiny little errors.

But well done anyway you pretty much nailed the task! hope I helped you out, Mark this as the answer if you agree it really helps me and lets others know there is a correct answer with a big green tick ahah. Just post a comment if you need anymore help.

Thanks Dom

this is a great answer :thumbsup:

Hah William Lee thanks buddy I am mastering swift from trawling the forum an answering questions, it's actually really helpful lol

Late to the party here, but you did more than just reformat Mia's code. You modified her designated initializer to accept sides and name (in addition to sideLength). She neglected to include those and it was throwing an error. Also, I found that I did not need to use "import Foundation" at the top and it worked fine. I just updated to Xcode 7.0.1 so maybe that's the difference.