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 trialAnthia Tillbury
3,388 PointsCustom Initialisers Code Challenge (Object Orientated Swift)
My code compiles with wifi v3 and Xcode v8 in the Playground, but not on the website:
struct RGBColor {
let red: Double
let green: Double
let blue: Double
let alpha: Double
let description: String
// Add your code below
init() {
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)"
}
}
let colourTable = RGBColor.init()
colourTable.description
I would like confirmation if I have completed the challenge as demanded:
In the editor, I’ve declared a struct named RGBColor that models a color object in the RGB space.
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"
ref: https://teamtreehouse.com/library/objectoriented-swift-2/complex-data-structures/custom-initializers
Thanks!
3 Answers
Mitch Little
11,870 PointsHi James!
The reason your solution compiles in Xcode is because there is no errors with your code. However the solution does not provide what the code challenge is asking you to do.
struct RGBColour {
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)"
}
}
let colour = RGBColour(red: 81.0, green: 181.0, blue: 243.0, alpha: 1.0)
You have set default values for the stored properties in RGBColor when you should be instead adding parameters for these in the initialiser method.
Init is also not a method you call upon an object. It essentially sets the values for all of the stored properties based on the arguments provided.
I would recommend comparing the solutions, and try again from memory after a few hours to fully make sure you understand and solidify your knowledge.
All the best!
Mitch
Anthia Tillbury
3,388 PointsI see immediately what I have done. I actually began writing out the properties in the Initialiser, comparing Initialised properties to .self, but then decided to declare them in the initialiser for some reason.
Thanks for your help!
Mitch Little
11,870 PointsAwesome! Always good to figure it out yourself.
No problem!
All the best,
Mitch