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 trialconnor hoare
7,933 PointsExplain why this is not compiling?
initialising and self
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
// Add your code below
init(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0) {
self.red = red
self.green = green
self.blue = blue
self.aplha = alpha
}
}
RBGColor.description = "red: \(red), green:\(green), blue: \(blue), alpha: \(alpha)"
1 Answer
Matthew Long
28,407 PointsYou have a few spelling errors and you should initialize the description as well. The spelling errors are alpha
instead of aplha
and you're missing a space between green: /(green)
.
If you write the code for these challenges inside Xcode instead of the challenges text editor then you'll notice these things much faster.
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 = 86.0
self.green = 191.0
self.blue = 131.0
self.alpha = 1.0
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
}
connor hoare
7,933 Pointsconnor hoare
7,933 PointsThanks,
I dont understand how self.description compiles though? I thought that adding self meant we are referring to the initialiser parameter then setting the default value to the stored property. e.g self.red
Matthew Long
28,407 PointsMatthew Long
28,407 PointsThe
description
property is not passed into theinit
method as a parameter, it is created from the other stored properties that are passed in as parameters. So, all instances of theRGBColor
struct
will have four numbers held as stored properties, the RGBA bits, and a nice string description that can all be accessed