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

Object-Orient Swift last task Help - Can't figure out what I'm doing wrong

Getting this error swift_lint.swift:23:30: error: cannot use instance member 'red' within property initializer; property initializers run before 'self' is available

structs.swift
struct RGBColor {
  let red: Double
  let green: Double
  let blue: Double
  let alpha: Double



  // 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

  }

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

2 Answers

tromben98
tromben98
13,273 Points

Hi Pino!

You probably removed the description property unintentionally, because when you start the challenge it starts with a description property in the struct.

Here is the solution:

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

    }

}

Best regards, Jonas

Ahhh, yes I see what I did...

Instead of moving the description variable after the init, I was simply supposed to self.description.

What i don't understand, and my confusion came from the fact that the description variable was set as a constant (let) meaning I couldn't change it.

Does the init method allow for changing let variables in an object?