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 trialsamuele carpani
4,219 PointsHow can i use init correctly?
I have difficulty with the method init, who help me?
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
}
description = "red:\(red),green:\(green),blue:\(blue),alpha:\(alpha)"
}
2 Answers
Brandon Mahoney
iOS Development with Swift Techdegree Graduate 30,149 PointsSo the numbers given are just an example, this needs to be reusable. So when you create and instance of the struct what ever values are fed in are what it will use. The struct should be as followed. You can place it in a playground and play around with it.
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
self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
}
let color = RGBColor.init(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)
print(color.description) //prints "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"
samuele carpani
4,219 PointsThanks you!
Brandon Mahoney
iOS Development with Swift Techdegree Graduate 30,149 PointsNo problem.
samuele carpani
4,219 Pointssamuele carpani
4,219 PointsThanks!