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

Zhi Huang
Zhi Huang
2,046 Points

Having trouble with this question...

Can someone tell me what i'm doing wrong with my code?

struct.swift
struct Expense {
  var description: String
  var amount: Double = 0.0
  init (description: String) {
      self.description = description
  }
  func calculateTaxes(percentage: Double) -> Double {
  var item = Expense(description: "Apples")
  item.amount = 100
  return self.amount * (percentage/100) 


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

}

2 Answers

The Error was : instead of Expense we had Expenses

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

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

// add the calculateTaxes method here
// it should accept only one parameter named 'percentage' of type Double

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

}
 }

    var item = Expense(description: "Money")
    item.amount = 100

You are on track. It is just that you were declaring the item variable inside of the struct

    struct Expenses {
      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 = Expenses(description: "Apple")
         item.amount = 100
Zhi Huang
Zhi Huang
2,046 Points

I have tried that. It still wont work :/

did you use my answer ?

Zhi Huang
Zhi Huang
2,046 Points

Yes i did, struct Expenses { 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 = Expenses(description: "Apple")
     item.amount = 100

can you repost the question that the task asked please ?

Zhi Huang
Zhi Huang
2,046 Points

Create a variable named item and assign it an instance of Expense (remember to use the initializer with the description parameter. Enter any description you like). On the next line assign the amount property a value of 100.

Did you make sure that you don't have many declarations in one line. because the code i gave you works .

Zhi Huang
Zhi Huang
2,046 Points

I know that the code works, but treehouse is saying that it doesn't? I tried the same code on xCode and it tells me that nothing is wrong with it. So therefore i copied and pasted your code, but it still wont work? Is there a way to report this?

can you take a screenshot and show me if you can ?

Zhi Huang
Zhi Huang
2,046 Points

[alt text](C:\ "treehouse")

i can't see the image ? post a link

can you click on the PREVIEW and send a screenshot ?

Zhi Huang
Zhi Huang
2,046 Points

WOW!... I really appreciate your help. Thank you so much for helping solve my problem.

Glad I could help ...