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

Sebastian Fernandez
Sebastian Fernandez
2,413 Points

i don't know what is wrong

pls help! y don't understand how to write the method

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

Hi Sebastian,

An initialiser method essentially sets the parameters for the Class when it is initialised and assigns the argument provides to the stored properties of the Class

Your initialiser method is correct, however you have hardcoded the values in so the stored properties will always remain as the value you have set.

You should assign the values taken in the Class initialiser to the stored properties:

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

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

This initialiser takes the arguments provided for its parameters and assigns them to the stored properties of the class.

When creating a class initialiser you must be exhaustive and provided values for all of the stored properties. The challenge then asks you to create a custom description for the description stored property. You can do this with string interpolation:

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

I believe the example provided by the challenge has confused the matter slightly.

I recommend that you take some time to go over this as it is constantly used in Swift and other Object-Oriented programming languages. Don't threat though, this course confused myself also but now it comes naturally.

All the best,

Mitch

You are very welcome dude!

Good luck!