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 trial

iOS Object-Oriented Swift 2.0 Complex Data Structures Custom Initializers

Joshua W
Joshua W
4,345 Points

cannot get pass the quiz

please see my code, thanks 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
    description = "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"
}

}

1 Answer

Nathan Tallack
Nathan Tallack
22,160 Points

Ok, let's take a look at how the struct is setup and how the code comes together.

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)"
    }
}

Key points are:

1: We declare our properties (red, green, blue, alpha, description) as part of the Struct.
2: We make our init method that takes in the passed arguments from the object creation.
3: See we are not passing description in because that is a computed property.
4: That computed property is why we need to make our own init method.

So then a user would create a RGBColor object with a command like this.

let bgColor = RGBColor(red: 86.0, green: 77.0, blue: 55.0, alpha: 128)

That computed property description would then look like this.

"red: 86.0, green: 77.0, blue: 55.0, alpha: 128.0"

I hope this helps clear it up for you. :)

OMG THANK YOU SO MUCH!!! y dont we need to put description: String in parentheses with red: Double and all the other ones