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

Jack Farr
Jack Farr
7,961 Points

No compiler error, not sure if this function within structure is written correctly.

Hi everyone,

Not sure why this is not what the question is looking for as it has a simple return. Have also tried including percentage in the initialize category.

Thanks!

Jack

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

    init (description: String, amount: Double, percentage: Double) {
        self.description = description
        self.amount = amount
    }

  func calculateTaxes (percentage: Double) -> Double {

return(self.amount * (percentage/100))
}
}

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

Don't modify the init method. Your calculateTaxes function is good, just substitute that into the code they initially gave you.

Think of it this way. The "amount" property doesn't need an initializer, it already comes with a default value (0.0). And "percentage" isn't a property of the expense struct, it's a parameter of one of it's methods, but it needs to get that information from the outside in order to calculate the taxes. (Kind of like, wait until I know what state I'm in to calculate the taxes, because it will vary).