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

how to change properties outside of struct

I am having trouble assigning the variable amount that is withing the expense struct to the value of 100. it keeps popping up with an error saying you can't assign this number because it is an int and "amount" calls for a double so i change it to int and it still didn't work. i think it must be because i'm assigning it the value wrong. Please help

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

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

  func calculateTaxes(percentage: Double) -> Double
  {

    return (self.amount*(percentage/100))
  }
  var item = Expense(description: "Big number")
   Expense(amount: 100)

}

1 Answer

Mark Gong-Guy
Mark Gong-Guy
2,830 Points
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: "Test")
item.amount = 100

Hello Drew:

Your code for this assignment should be as above. Remember that when you are defining the item you probably want to define it outside the struct itself. The other thing that is really you problem is that to set a value to the struct value amount you need to call it like I did above.

If you have any questions or feel free to let me know!

Mark :)

Thank you so much this helped so much! Really appreciate you taking the time to answer it!