Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.