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 trialRich Valiant
2,417 PointsHow to add a String of values to let description property?
I've watched each video in the series several times and I thought to create a method which returns a string and then add the result to the description variable but its a let property constant so I can't. I'm lost on how to accomplish this step. Any help would be appreciated.
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
init(red: Double, green: Double, blue: Double, alpha: Double) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
let description: String
// Add your code below
func something() {
self.description = "red: \(red), green: \(green), blue: \(blue), alpha \(alpha)"
return description
}
}
1 Answer
jcorum
71,830 PointsSergio, Hi! You need to create a custom initializer that gives a value to each of the 4 stored properties:
init(red: Double, green: Double, blue: Double, alpha: Double) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
Once the stored properties have a value, then description will as well, as it has access to the values stored in those properties.
So if you create a RGBColor object:
let color = RGBColor(red: 23/255, green: 235/255, blue: 155/255, alpha: 0.8)
print(color.description)
The value of red will be 23/255, ..., and description can use those values. What will be displayed (printed) in an Xcode playground will be this:
"red: 0.0901960784313725, green: 0.92156862745098, blue: 0.607843137254902, alpha: 0.8\n"
Becky Christofferson
15,047 PointsBecky Christofferson
15,047 PointsThanks! That helped me!
Rich Valiant
2,417 PointsRich Valiant
2,417 PointsHello jcorum,
Thank you so much. Very thorough explanation to the final print detail of color.description. Really illuminated it all in practice and in theory.
Got it!