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

Nikolas Tamvakis
Nikolas Tamvakis
2,281 Points

Using the values assigned to those properties create a value for the description property

I am not sure what I am doing wrong. When I try this, it tells me I cannot use "red" within the property initializer so I think that the way I have it uses "description" as a parameter but I am not sure how to keep it from doing that (or if that is even what I need to do). Please give me some advice.

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

  let description: String

  // Add your code below
}
*/

struct RGBColor {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double

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

    let description: String = "red: \(red), green:\(green), blue:\(blue), alpha:\(alpha)"
}
eh wah
eh wah
2,280 Points

I have the same problem

eh wah
eh wah
2,280 Points

this is one of correct answer from Dhanish Gajjar, and I still don't really understand it

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


}
let newColor = RGBColor(red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0)
newColor.description
          ```

1 Answer

Hurkan Dogan
Hurkan Dogan
8,099 Points

You don't need to add a new value named newColor. Delete the bottom two lines and you have the correct answer.

Hurkan Dogan
Hurkan Dogan
8,099 Points
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)"
  } 
}