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

Johnny Nguyen
Johnny Nguyen
3,875 Points

What to do next?

I can't understand what the task is about

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, description: String) {
        self.red = red
        self.green = green
        self.blue = blue
        self.alpha = alpha

        }
}

2 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Johnny Nguyen,

You're so close!! Just misreading the challenge on one part (had to do the challenge myself to confirm that!)

Your task is to write a custom initializer method for the object. Using the initializer assign values to the first four properties.

You basically nailed this part, but added a 5th property - description.

// Challenge: first four (4) properties (r/g/b/a). Got to remove that description
init (red: Double, green: Double, blue: Double, alpha: Double, description: String) {
        self.red = red
        self.green = green
        self.blue = blue
        self.alpha = alpha
}

Next part of the challenge

Using the values assigned to those properties create a value for the description property that is a string representation of the color object. ... description property should look like this: "red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"

Let's use some string interpolation and build it to the specs of the example given

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

Pop that into your initializer method and you should be laughing.
Hope this helps you out mate.

Great effort! and keep on coding :dizzy:
Dave

Johnny Nguyen
Johnny Nguyen
3,875 Points

I've already found an answer for myself. Thank you anyway!