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

Whenever I try to run my program it crashes.

Can somebody please help me fix these crashes or atlas explain what is causing them.

Here is my code:

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)
    }
}  // <-- I added the closing }  Steve. :-) 

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

10 Answers

There doesnt seem much wrong with that except you haven't closed your Clothing class before you declare your variable. Add another closing brace above the tshirt declaration.

Let me kow if that doesn't get it sorted.

Steve.

I added the closing brace and a comment in your code.

What errors are you seeing in your code?

It is still crashing, I tried updating the software even using the downloaded file and neither worked. I think it is a bug.

What error are you getting?

Extraneous '_' in parameter: 'percentage ' has no keyword argument name

Delete the _ then?

I tried it still crashes

I think it is because the lately update. It says 'Expected Declaration' having the same issue

import UIKit

class Product {
    var 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, Mediu, Large
    init() {
        self = .Small
    }
}



class Clothing: Product {
    var size = Size()
    override func discountedPrice(percentage: Double) -> Double  {
        return price - (price * percentage / 100)

        }

let tshirt = Clothing(title: "T-Shirt", price: 344.99)




let quadcorpet = Product(title: "Quadcorpet", price: 23.99)

So do you think I should just wait for it to be fixed.

I'm getting the same error. I noticed that Xcode only freaks out when I try to set a default value for the 'percentage' argument. The code challenge following this video doesn't seem to like it either...

Do you know anywhere where I can downgrade Xcode?

I think you can remove updates inside the App Store; it lists the updates you've applied - I think you can disapply them there. Never tried, though.

Steve.

I won't show all my updates it just shows two. Do you think I could get it from Time Machine?

I asked a question on Stack Overflow maybe someone there knows how to fix it.