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 Classes and Objects Classes and Their Methods

Joe Dayvie
Joe Dayvie
11,956 Points

My code in Xcode not working...

Hello,

Below is my code:

class Product {
    let title: String
    let 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)
    }
}

let quadcopter = Product(title: "Quadcopter", price: 499.99)
quadcopter.discountedPrice(50)

Where I seem to have an issue is the self.price = price since it give me an error. I went through and it all appears to match and when I retype it, the error goes away but then its unusable.

Does anyone have any insight? Thank you!

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Joe Dayvie

In this line here

let price: Double = 0.0

the price property was given a default value of 0.0, I believe Swift won't allow you to reassign another value to a constant even in the init method.

Change the let to var then it should work fine.

Now, you may ask why Amit typed the exact same codes during the video and it worked without error; please keep in mind that Swift is a very very new language, it's constantly changing and evolving, as a matter of fact, it has gone through several major updates since the video was recorded. It's possible that Apple introduced some changes to the language that break the existing code, because the Swift you had in the latest Xcode is a much newer version than what Amit used during the course video. Hope this helps.

Joe Dayvie
Joe Dayvie
11,956 Points

William,

Perfect! I appreciate it very much.

I knew Swift is very new but I am realizing just how many changes have happened within a year. Its tough to keep up when someone is as new as I am.

I appreciate it again =)

Joe

William Li
William Li
Courses Plus Student 26,868 Points

Yep, in all honesty, I'm a bit surprised about how hard-working Apple has been on improving the Swift language in the past several months.

But I think it makes totally sense that they made bold changes & improvements to the language at this early stage; it'd be much harder to do so a couple years later when many high-profile Swift projects have been written, there's no way Apple can introduce a breaking change to the language without upsetting everyone.

Joe Dayvie
Joe Dayvie
11,956 Points

William,

Completely agree - If they are going to do it well, make the necessary changes early on instead of dragging it out years ahead.