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

Explain why this is not compiling?

initialising and self

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: 86.0, green: 191.0, blue: 131.0, alpha: 1.0) { 
self.red = red
self.green = green
self.blue = blue
self.aplha = alpha

  }

}

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

1 Answer

Matthew Long
Matthew Long
28,407 Points

You have a few spelling errors and you should initialize the description as well. The spelling errors are alpha instead of aplha and you're missing a space between green: /(green).

If you write the code for these challenges inside Xcode instead of the challenges text editor then you'll notice these things much faster.

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
    self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
  }
}

Thanks,

I dont understand how self.description compiles though? I thought that adding self meant we are referring to the initialiser parameter then setting the default value to the stored property. e.g self.red

Matthew Long
Matthew Long
28,407 Points

The description property is not passed into the init method as a parameter, it is created from the other stored properties that are passed in as parameters. So, all instances of the RGBColor struct will have four numbers held as stored properties, the RGBA bits, and a nice string description that can all be accessed