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 Complex Data Structures Custom Initializers

Maxence Roy
Maxence Roy
8,753 Points

Help please

I'm pretty sure I got a couple things wrong here. I'm having a hard time with Object Oriented programming.

structs.swift
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
    }

    func descriptionString() -> String {
        let description: String = "red:" + red + ", green:" + green + ", blue:" + blue + ", alpha:" + alpha
        return description
    }
}

RGBColor.descriptionString()

3 Answers

Hey Max,

I also had a bit of trouble figuring this one out too, but it kinda made sense in the end, the challenges can be very particular with what they are looking for.

your first part setting up the Init method is almost right, however we dont have to assign any values to it. Think of this Init method as a blueprint for all other instances (copies) of the RGBColour struct. So inside this Init method we are assigning red to red, blue to blue etc so it should look like this:

    init(red: Double, green: Double, blue: Double, alpha: Double) {
        self.red = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
    }

Finally the part that confused me a little was adding in the description, i assumed it would have to be set up like

init(red: Double, green: Double, blue: Double, alpha: Double, description: String) {

However this is not the case, my understanding is the concatenation of the description is set up inside the Init method however we do not have to specify it as a parameter (it is passed in via the struct?)

So my final code looked like this:

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

Hope this helps =]

Domenico Mazzella Di Bosco
Domenico Mazzella Di Bosco
2,357 Points

This line cleared it up - "the concatenation of the description is set up inside the Init method however we do not have to specify it as a parameter (it is passed in via the struct?)"

thanks