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 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,021 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!