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 Initializers

Add an init method to the Expense struct so we have to only pass the description when creating an instance. The init met

Add an init method to the Expense struct so we have to only pass the description when creating an instance. The init method should accept only one parameter named description which will be assigned to the stored property description within the init method.

Watched video repeatedly, don't have a clear understanding what I am being asked

5 Answers

Solved it. It was the quotes. leave them out. It only made sense to me when amit used 'fname' in an example to show minimise confusion but it never jumped out and punched me in the nose. Once I know what I am doing I'm going to use all caps or something similar to avoid confusion. The correct working code for others is below. Thanks for your help

struct Expense {
    var description: String = "description"
    var amount: Double = 0.0       

       init(description :  String) {
        self.description = description
    }
}

Hi Kaitlan,

The question is asking you to create an initializer that takes 'description' as a parameter, then use the self keyword to assign the parameter to the Expense struct's own description variable.

As 'amount' already has a default value, creating an instance of the Expense struct will only require you to specify the init(description) parameter.

Hope this points you in the right direction :-)

Tim

Hello, thanks for the reply. well I think I worked out what question wanted thanks, I can't work out why though what I've done that is not working. The error I get is: " Remember to assign the description parameter to the stored property named 'description' within the 'init' method"

struct Expense {
    var description: String = "description"
    var amount: Double = 0.0       

       init(description :  String) {
        self.description = "description"
    }
}

There is no point in assigning a value to description in the second line. Or is there?

jamesdei
jamesdei
6,738 Points

Don't put description between quotes inside the function and it will work. It's a variable not a string. (its value is a string).

ellie adam
ellie adam
26,376 Points
 init(description: String) {
      self.description = description
    }