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

Wei Luo
Wei Luo
1,372 Points

what is the problem of my code??

Don't know what the task want me to do..

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, description: String) {

  self.red = red
  self.green = green
  self.blue = blue
  self.alpha = alpha

  }
   let description = RGBColor(red: 847.0, green: 191.0, blue: 131.0, alpha: 1.0) // Add your code below
}

1 Answer

Everything is fine up to where you create the initializer. You need to separate creating it from calling it.

Here's what the constructor should look like:

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

And here's how you would call it, and then use the computed property description:

let color = RGBColor(red: 23/255, green: 235/255, blue: 155/255, alpha: 0.8)
print(color.description)

In Xcode the last statement would display this:

red: 0.0901960784313725, green: 0.92156862745098, blue: 0.607843137254902, alpha: 0.8