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 trialkaitlanlang
3,042 PointsAdd 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
kaitlanlang
3,042 PointsSolved 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
}
}
Timothy OBrien
2,832 PointsHi 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
kaitlanlang
3,042 PointsHello, 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"
}
}
Konstantin Mishukov
7,707 PointsThere is no point in assigning a value to description in the second line. Or is there?
jamesdei
6,738 PointsDon'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
26,377 Points init(description: String) {
self.description = description
}