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

Ryan Servais
Ryan Servais
1,350 Points

For the last step, I don't exactly understand how to provide this new amount definition within my struct instance.

I figured you could just include this new definition of amount within the parentheses. I guess I really just don't understand the proper syntax to answer this question

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: "New Amount", amount: 100)
}

1 Answer

Hey Ryan,

I was stuck on this too, so I figured I could help out if you still are.

Because we used an "init" method in the Expense struct that only set a required parameter for Description, we can only define the description parameter for a new instance of Expense in parentheses; amount has to be defined separately.

To change the value of amount call item.amount and set it equal to 100 (or anything else you like!

My code is below if you find it helpful :)

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: "Rental Car")
item.amount = 100