Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Structs and their Methods Struct Methods

Struct Method Task 3 of 4

Is anyone else having trouble moving past Struct Method task 3 of 4? The preview isn't showing anything as wrong.

struct.swift
struct Expense {
    var description: String
    var amount: Double = 0.0

    init (description: String) {
        self.description = description
    }

func calculateTaxes (percentage: Double) -> Double {
    return (self.amount * (percentage/100))
    var item = Expense (description: "Whatever works")
    let amount = 100
  }

}
Chengyu Deng
Chengyu Deng
7,879 Points

The "amount" is a property of the struct Expense. To get access to this property you should use item.amount instead of just amount. If you just type in "let amount = 100", it just means that you create a constant named amount and its value is 100. Hope this helps!

1 Answer

Rick Matsumoto
Rick Matsumoto
2,547 Points

This entire module has been tough for me! I agree with the previous post, but here's the code where I actually got it to work.

struct Expense {
    var description: String
    var amount: Double = 0.0

    init (description: String) {
        self.description = description
    }

    func calculateTaxes(percentage: Double) -> Double {
        return (self.amount * (percentage/100))
    }
}

var item = Expense (description: "any expense")
item.amount = 100