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 trialVon Chucwuemeca
4,746 PointsInitializers - I've reviewed my code and it should work, however I may be missing something.
The playground informs me that I should not use memberwise initializers, however I've used custom variables for the initializer.
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
// Add your code below
init(colorRed: Double = 86.0, colorGreen: Double = 191.0, colorBlue: Double = 131.0, alphaColor: Double = 1.0, theDescription: String = "green") {
self.red = colorRed
self.green = colorGreen
self.blue = colorBlue
self.alpha = alphaColor
self.description = "red: \(colorRed), green: \(colorGreen) blue: \(colorBlue) alpha: \(alphaColor)"
}
}
2 Answers
kasaltrix
9,491 PointsAlthough this code is right in it's own way, the question doesn't ask for it. It is clearly given in the question that you should declare a custom initializer rather than using the default initializer provided by Swift.
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.
According to the question, the code can be modified as below-
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
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)"
}
}
Hope this helps :)
Von Chucwuemeca
4,746 PointsOk, I thought writing my own customer initializers meant using custom terms. I now understand, thanks.