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

Josh Smith
Josh Smith
1,049 Points

Return Going Wrong?

I think I'm not outlining the return statement correctly and or putting the method together right. I could use some guidance!

struct.swift
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) {
   (self.amount * (percentage/100))
   return calculateTaxes.Double

}


}

2 Answers

Hey Josh, I looked a little further. In my weary eyed look earlier, I forget a couple of things...

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 { //Line 1
        return self.amount * (percentage/100) //Line 2
    }


}

Line 1: Notice that you use the arrow notation to denote the return type. This is the type of data that your function is "spitting out". Oops

Line 2: Also, Swift is a little touchy when you cuddle operands and operators. There has been a change from

return self.amount*(percentage/100)

to

return self.amount * (percentage/100) //Notice the spaces surrounding the *.

Hope this gets things working for you.

Josh Smith
Josh Smith
1,049 Points

WOOO that worked, been stuck for hours haha! Thank you Justin :D part 3 of 4 looms... god help me lol

Hey Josh, You are on the right track, but we just need to tweak your method a little so we can return the calculated value... Let's take a look:

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) {
   return self.amount*(percentage/100) //notice that you are dividing percentage by 100 to get a decimal then          
                              //multiplying by the self.amount value of Expense. You put this after the 
                              //return statement because this is the amount that you want to "spit out"          
                              //(return) 

}


}

Hopefully this is will get your method working.

Josh Smith
Josh Smith
1,049 Points

Hi Justin,

Thanks so much for getting back to me! I tried running that code but it couldn't be compiled so I'm not sure whats wrong it it?

Hey Josh, What exactly is the error that you are getting? You may want to remove my comments if you have those in there.

Josh Smith
Josh Smith
1,049 Points

Hi Justin, this is the error message in Editor:

swift_lint.swift:12:23: error: cannot invoke '/' with an argument list of type '(Double, ($T6))' return self.amount * (percentage/100) ~~~~~~~~~~^~~~~~~~~~~~~~~~