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 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...