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

Mark Weiser
Mark Weiser
2,093 Points

Trouble with 'init' on step 2

I added an init for "amount" variable but it doesn't seem to read. Stuck in the mud on this one!

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

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


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

1 Answer

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

The variable description needs to be initialized.

You can: Assign it a value: var description = " "

or

Make it an optional: var description: String?

Also check your return type for your function.

The function signature states it returns a double, but your code returns a string.

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

or

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

Mark Weiser
Mark Weiser
2,093 Points

Thanks for the help Chris. I actually also tried

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

last night and it wasn't working. Tried it today and it worked fine, so I'm thinking it was a Treehouse glitch.