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 trialsreeputturi
1,355 Pointsstuck in coding challenge of Initializers and self.. help please!
The clue says custom init and I am trying to do that, but does not seem to go far.. can I get a few more clues as to what I am doing wrong?
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)
{
red = 86.0
green = 191.0
blue = 131.0
alpha = 1.0
self.description = "red:" + "\(red)" + ",green:" + "\(green)" + ",blue:" + "\(blue)" + ",alpha" + "\(alpha)"
}
}
1 Answer
Steven Deutsch
21,046 PointsHey sreeputturi,
You're going to initialize your RGBColor struct with the values that you pass into the initializer method. So, you set up the parameters of the init() method properly, now you just need to assign those values to the stored properties of your struct. You can do this by using self to refer to the property of the struct that you want to assign a value to.
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) {
/* we use self to refer to the property of the struct.
self is used to differentiate between the parameter name and the property name.
if we did red = red, Swift wouldn't know which red we're referring to */
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
/* I updated your description to be a single string instead of concatenating
as this is easier to get the spacing correct */
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
}
Hope this helps! Good Luck!
sreeputturi
1,355 Pointssreeputturi
1,355 PointsIt does. thanks much. The data that was in the challenge mis-lead me into thinking I could initialize within the struct.. dumb move.. Thanks Steven!