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 trialBilly Potts
Courses Plus Student 2,344 Pointshow do i create my own custom initializer
how do i create a custom initializer and not swifts member wise one
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, description: String) {
self.red = 86.0
self.green = 191.0
self.blue = 131.0
self.alpha = 1.0
self.description = "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"
}
}
2 Answers
omarelamrieado
18,473 PointsUse a class, classes do not have an automatic member wise initializer. Also, they support inheritance, more the reason to use a class. If any of this sounds like jibberish, continue with the Object Oriented Swift course, as it will teach you more about structs and classes.
Edit:
Oh, I did not know that this was a challenge that specifies that you need a struct, please disregard.
omarelamrieado
18,473 PointsReal answer this time:
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)"
}
You were on the right track, but the values Pasan Premaratne provided were only for an example.
So, instead of having a fixed values for an object, (having fixed values defeats the the purpose of Object Oriented Programming) set them to the attributes of the instance.
For the description, same case. Instead of a fixed description, interpolate the default string to have the attributes of the object.
Billy Potts
Courses Plus Student 2,344 PointsBilly Potts
Courses Plus Student 2,344 Pointsyea i don't understand and it won't let me move on until i complete this task
omarelamrieado
18,473 Pointsomarelamrieado
18,473 PointsLook at the answer below.