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

Struct Method - Swift Enums and Structs

Hey all. I'm having this issue with the challenge on Swift Enums and Structs course. My code is compiling with no errors but I'm still getting an error when submitting the answer. Thanks in advance!

Add a method to the Expense struct that will calculate taxes based on a percentage passed to the method. In this task, add a method named calculateTaxes which accepts a parameter named 'percentage' of type Double and does not return anything. (In the next task we will add the calculations to the method).

Add a Double as a return type to the calculateTaxes method and within the method return the following expression: (self.amount * (percentage/100)).

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

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

    func calculateTaxes(percentage: Double) -> Double {
    return (self.amount * (percentage/100))

 }
}

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Tony,

You have a minor error in your code which is you've specified a new description property in the struct which isn't required as description is a parameter so it's already assigned as a contant within the method.

The final code for task 2 you should have is as follows.

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

Happy coding!

Thank you!