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 trialVera Hsu
997 PointsHaving troubles with initializer
I don't know what is wrong with my code. Bummer said "Don't use the member wise initializer Swift creates. Add your own custom initializer as specified in the directions" which I don't quit get its meaning. Any help would be great. Thanks.
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
// Add your code below
init (red: Double, green: Double, blue: Double, alpha: Double, description: String) {
self.red = 86.0
self.green = 191.0
self.blue = 131.0
self.alpha = 1.0
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
}
2 Answers
Steve Hunter
57,712 PointsAh, yes, but you do initialize description
- you just don't pass a value in. The constructor will assign a value to description
- just like you have already with this:
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
But to form that string you don't need to pass a value in for description
, it is made up of the values passed in for the other four properties. So, the compiler will be happy; description
holds a value that is formed by the other four values that are passed into the constructor.
You don't need to pass values to a constructor; you can assign values to properties. In your existing code, you have assigned values to all five stored properties without using any of the values you passed in. So, this is valid:
init () {
self.red = 86.0
self.green = 191.0
self.blue = 131.0
self.alpha = 1.0
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
No parameters but all stored properties are initialized.
What we need here is a middle ground - pass in some values but not all:
init (red: Double, green: Double, blue: Double, alpha: Double) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
OK with that?
Steve.
Steve Hunter
57,712 PointsHi Vera,
You don't need to pass description
in as a parameter to the contructor. The constructor itself generates the content that is stored in the description
property. You don't have a value for it so can't pass it in.
Also, the values for the other elements are given by way of example; don't hard code those inside the constructor. So, each RGBA value will be passed into the contructor as the parameters, then assigned to the stored properties inside the method. You've got red: Double
being passed into the constructor; inside it you will then say that self.red = red
to assign the value sent in; not the hard-coded example figures.
Does that make sense?
Let me know how you get on.
Steve.
Vera Hsu
997 PointsBut if I don't initialize description, there will be a compile error about "didn't initialize all the stored properties..." somethings like that. What do I do with that?? thanks
Vera Hsu
997 PointsVera Hsu
997 PointsI understood. This part is a little tricky and hard for me to understand haha. Huge thanks to you, Steve. Thanks for your explicit explanation!!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThis one is a difficult challenge, yes! I'm glad you understand it and got through.