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

this

too hard

structs.swift
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 = 86.0
        self.green = 191.0
        self.blue =  131.0
        self.alpha = 1.0
    }
}

1 Answer

Hey Trevor,

It looks like you're trying to set the value in the initializer itself rather than setting up the initializer properly and setting the value when you use the struct.

What you need to do is setup the initializer to take the properties for red, green, blue and alpha when you go to initialize it:

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

Basically, you're telling the struct's red property to match the one in your initializer, your green property to match the one in your initializer and so on.

It's a hard concept to communicate and I had trouble wrapping my brain around it too but it helped me to put my cursor in red for self.red and see where else the property is used by Xcode's highlighting and then put my cursor in red after the assignment operator = to see where that is used.

To the left of the = you're referring to the struct, to the right you're referring to the init function.

Then, when you initialize the struct you can specify the actual value for that instance of the struct.

let myColor = RGBColor(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)

Here is what I used to complete the challenge:

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

        description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
    }
}

I hope that helps, if it's still confusing let me know and I'll try to clarify further, it would help to know a little more about where you're getting stuck mentally so we can narrow in a little more.