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

What is wrong with my last line of code? I think that's what I have to do. Is my syntax incorrect?

Is the problem coming from any other line of code?

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: "Hello")
self.amount = 100

1 Answer

Greg Kaleka
Greg Kaleka
39,019 Points

Hi Karl,

The self keyword is for use inside Struct and Class definitions as a sort of placeholder for an instance of the Struct or Class. Once you're outside the definition, though, you want to use the actual instance you've created!

In your code, you created an instance of Expense and stored it in the variable item. You should then set the property amount of that particular instance, like this:

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: "Hello")
item.amount = 100 // item is an instance of Expense!

Thanks so much, Greg!