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

Struct Methods Challenge 4/4 Can't get it

I'm stuck on this last question in the structs methods challenges. The instructions are: to figure out the tax amount when the tax percentage is 7.5 percent. Call the method calculateTaxes on the variable item and assign the result to a new variable named taxes. Can anyone please help me?

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))


    }
    // it should accept only one parameter named 'percentage' of type Double

}

var item = Expense(description: "Taxes"); item.amount = 100
var taxes = item(calculateTaxes); taxes.amount = 7.5

2 Answers

Task 4 states: . Call the method calculateTaxes on the variable item and assign the result to a new variable named taxes

you can call a function on an instance of a struct using dot syntax.

someInstance.someFunction()

in this case you need to call the function calculateTaxes on your item instance and pass in 7.5 as the argument. you need to assign the return value of that to a variable named taxes. it should look something like this

var taxes = item.calculateTaxes(7.5)

Thank you Stone- my brain is fried so thanks for providing another set of eyes!

I've been working on this in Xcode and I'm not getting an error code when I use:

var taxes = Expense.calculateTaxes(item)

What is this line of code actually doing? I'm calling calculateTaxes on Expense and passing it item? I can't get it to println() a result no matter what I try.