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 Properties Computed Properties

Christiaan Hendriksen
Christiaan Hendriksen
5,982 Points

Code from previous stage fails in Xcode

I've downloaded the code from the last exercise (included at the end of this question) but when I try to run it in the playground I get the following error:

Communication with the playground service was interrupted unexpectedly

Here's the code I'm using:

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

enum Size {
    case Small, Medium, Large
    init() {
        self = .Small
    }
}

class Clothing: Product {
    var size = Size()
    let designer: String

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

    convenience init (title: String) {
        self.init(title:title,price:99,designer:"Calvin Klein")
    }

    override func discountedPrice(_ percentage: Double = 10.0) -> Double {
        return super.discountedPrice(percentage)
    }
}

var tshirt = Clothing(title: "Vintage")
tshirt.title
tshirt.price
tshirt.designer
tshirt.discountedPrice(50)
tshirt.size

let quadcopter = Product(title: "Quadcopter", price: 499.99)
Melc Sokat
Melc Sokat
1,310 Points

<code> 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) {
    price = price * percentage / 100
}

} enum Size { case Small, Medium, Large

init () {
self = .Small
}

} class Clothing : Product{ var size = Size() override func discountedPrice(_ percentage:Double = 10.0 ) { return super.discountedPrice(percentage) } }

var tshirt = Clothing(title: "Vintage", price: 49.99) tshirt.title tshirt.price tshirt.discountedPrice(75) tshirt.size

let quadcopeter = Product(title: "Quadcopter", price : 499.99)

</code> This is working :)

Melc Sokat
Melc Sokat
1,310 Points

Got no ideea why the code is not formatting :( i try with html tag code... not working.

2 Answers

Having the same problem. "Communication with the playground service was interrupted unexpectedly."

Edgar Barajas
Edgar Barajas
6,906 Points

I think it crashes because of the overriding method. The underscore in the parameter makes is fail. I'm guessing the syntax changed in swift 2.0.