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 Inheritance Overriding Methods

boris said
boris said
3,607 Points

Can somebody please help me my all my program is doing is just saying times: then rapidly counting up.

Here is my 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 = 10.0)-> Double {

return price - (price * percentage / 100) }

enum Size {

    case Small, Medium, Large

    init() {

        self = .Medium
    }

}

}

class Clothing: Product {

var size = Size()

 override func discountedPrice(percentage: Double)-> Double {

    return price - (price * percentage / 100)

}

var tshirt = Clothing(title: "Tshirt", price: 500)

1 Answer

I copied your code in Xcode and it came out like this:

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 = 10.0)-> Double {
return price - (price * percentage / 100) }

enum Size {

    case Small, Medium, Large

    init() {

        self = .Medium
    }

}
}

class Clothing: Product {

var size = Size()

 override func discountedPrice(percentage: Double)-> Double {

    return price - (price * percentage / 100)

}
var tshirt = Clothing(title: "Tshirt", price: 500)

Now if this is correct the errors I found was you enum should be outside the Product class. You have it nested in. A you forgot a closing brace to close the Clothing class after you overrided the function. You also put the 10% discount with the original discountedPrice method instead of when you tried to override it.

import Cocoa

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 = .Medium
    }

}

class Clothing: Product {

    var size = Size()

    override func discountedPrice(_ percentage: Double = 10.0) -> Double {

        return price - (price * percentage / 100)

    }
}

var tshirt = Clothing(title: "T-shirt", price: 500.00)

Overriding isn't working for me on Xcode 7. Got a number of errors. But based on the course instructions at the moment, overriding should look like this. I'll be happy with the Swift 2.0 (though now it 2.1) version of the course is released.