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 trialIndia Poetzscher
Courses Plus Student 3,821 PointsI'm stuck and I can't figure this out!
Your task is to write a custom initializer method for the object. Using the initializer assign values to the first four properties. Using the values assigned to those properties create a value for the description property that is a string representation of the color object.
For example, given the values 86.0 for red, 191.0 for green, 131.0 for blue and 1.0 for alpha, each of the stored properties should hold these values and the description property should look like this:
"red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"
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) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
description = "\(red: Double, green: Double, blue: Double, alpha: Double)"
}
3 Answers
Connor Walker
17,985 PointsYou're on the right track but there are a couple problems with your code.
First problem is that you are assigning the value to the description variable outside of the init constructor. This can be seen in the error message in the output console
swift_lint.swift:24:1: error: expected declaration
description = "\(red: Double, green: Double, blue: Double, alpha: Double)"
^
The next problem is with the interpolation itself. The challenge wants something along the lines of this:
description = "red: \(value), green: \(value) ..." etc
But you have tried to just interpolate all of the variables and their type instead. If you make these changes it should pass fine
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) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
}
Steven Parker
231,268 PointsYou're pretty close! But the description should be set inside the "init" method with the other properties.
Also, you'll need to use literals for each property name in addition to the property values to get it to look like what the instructions asked for (like "red: \(red), "
... etc.).
India Poetzscher
Courses Plus Student 3,821 PointsThank you! I passed!