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

I'm getting multiple errors for the code in the "Getter and Setter methods" video?!

Here's my code which matches that in the video. What's am I doing wrong????

class Product {
    let title: String
    var price: Double = 0.0

    init(title: String, price: Double) {
        self.title = title
        self.price = price
    }

    func discountedPrice(percentage: Double) -> Double {
        return price - (price * percentage / 100)
    }
}

class Furniture: Product {
    var height: Double
    var width: Double
    var length: Double
    var surfaceArea: Double  {
        get {
            return length * width
        }
        set {
            length = sqrt(newValue)
            width = sqrt(newValue)
        }


    init (title: String,
        price: Double,
        height: Double,
        width: Double,
        length: Double) {
            self.height = height
            self.width = width
            self.length = length
            super.init(title: title, price: price)
    }
}

let table = Furniture(title: "C Table", price: 300, height: 5, width: 5, length: 10)

table.surfaceArea = 144
table.width

1 Answer

You need to import UIKit into your playground file for this code to be error free. Then it should match:

import UIKit

class Product {
    let title: String
    var price: Double = 0.0

    init(title: String, price: Double) {
        self.title = title
        self.price = price
    }

    func discountedPrice(percentage: Double) -> Double {
        return price - (price * percentage / 100)
    }
}

class Furniture: Product {
    var height: Double
    var width: Double
    var length: Double
    var surfaceArea: Double {
        get {
            return length * width
        }
        set {
            length = sqrt(newValue)
            width = sqrt(newValue)
        }
    }
    init (title: String, price: Double, height: Double, width: Double, length: Double) {
        self.height = height
        self.width = width
        self.length = length
        super.init(title: title, price: price)
    }
}
let table = Furniture(title: "Coffee Table", price: 300, height: 5, width: 10, length: 10)
table.surfaceArea = 144
table.width
table.length
table.surfaceArea

It wasn't the UIKit (I just forgot to copy and paste that over). I forgot a '}' after the setter. Your code help me figure it out so thanks!