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 Review Structs

Jason S
Jason S
16,247 Points

Add a default value of 0.0 for the amount property of the Expense struct.

struct Expense { var description: String var amount: 0.0 }

why doesn't this work?

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

2 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Jason!

You will add a default value in Swift for a variable like this:

struct Expense {
    var description: String
    var amount: Double = 0.0
}
Sam Chaudry
Sam Chaudry
25,519 Points

Hi Jason,

I've just run through your code in Xcode and this is what you are after.

//Set the struct struct Expense {

var description: String

// Your answer var amount: Double = 0.0

}

//Initialise and added my own values to it for a demo var expenses = Expense(description: "Sam", amount: 33);

//Prints out {description "Sam", amount 33.0}

In Swift you just need to remember that when your declaring your variables, you need to write the type of variable first i.e var or let. Then the type of the value, so this can be an int, float, double etc and then set the value it equals, so 0.0 in this case.

I have initialised it so you can see how it performs and what it outputs as an example. Hope this helps...