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 Stored Properties

How Is This Wrong?

When checked, the system tells me that it can't be compiled. I am wondering what I did wrong.

struct.swift
struct Expense {
    var description = "String"
    var amount = "Double"
}
var travel = Expense(description: "Flight to Cupertino", amount: 500.00)
Knut Olaf Lien
Knut Olaf Lien
7,743 Points

Did you mean to pass inn amount as String or as Double type? Try this:

struct Expense {
    var description = "String"
    var amount = "Double"
}
var travel = Expense(description: "Flight to Cupertino", amount: "500.00")

Or this:

struct Expense {
    var description = "String"
    var amount = 123.0
}
var travel = Expense(description: "Flight to Cupertino", amount: 500.0)

2 Answers

Greg Kaleka
Greg Kaleka
39,019 Points

Hi Rhew,

In your struct definition, you are setting default values for the two properties of the string literals "String" and "Double". Note you're setting them to the strings "String" and "Double", not the data types String and Double. This would be just like setting them to "Potato" and "Monkey". Not only is this not what you're trying to do, but for this challenge, there's no need to set default values.

What you want to instead of assigning them values define their types. You do this with a colon, and since String and Double are keywords, there's no need for quotation marks (in fact you need to not use them).

solution.swift
struct Expense {
    var description: String
    var amount: Double
}

var travel = Expense(description: "Flight to Cupertino", amount: 500.00)

We're declaring the variables and just telling Swift what kind of variables they should be when initialized.

Let me know if this makes sense!

Best,

Greg

Knut Olaf Lien
Knut Olaf Lien
7,743 Points

Note that you can set default values in a struct if this is what you want:

struct Expense {
    var description = "Flight to Cupertino"
    var amount = 500.0
}

var travel = Expense()

This will set the same values every time you declare an Expense variable. You can still override its values if you need.

Greg Kaleka
Greg Kaleka
39,019 Points

Thanks Knut - you're correct. I've updated my answer in light of this.