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

Dhruv Mittal
Dhruv Mittal
932 Points

Can't solve

In the editor, I’ve declared a struct named RGBColor that models a color object in the RGB space.

Your task is to write a custom initializer method for the object. Using the initializer assign values to the first four properties. Using the values assigned to those properties create a value for the description property that is a string representation of the color object.

For example, given the values 86.0 for red, 191.0 for green, 131.0 for blue and 1.0 for alpha, each of the stored properties should hold these values and the description property should look like this:

"red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"

Note: Init methods typically list parameters in the same order of property declaration. For this task, stick to the order red,green,blue,alpha.

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

1 Answer

Damien Watson
Damien Watson
27,419 Points

Hi, you need to define the description inside the init function, not pass it in, try this:

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)" 
    }
}
Dhruv Mittal
Dhruv Mittal
932 Points

Thank you for the reply!

I was still a little confused as to why you did not include description inside the init parenthesis

Your answer: init(red: Double, green: Double, blue: Double, alpha: Double)

why is this not: init(red: Double, green: Double, blue: Double, alpha: Double, description: String)

It would be very appreciated if you could help clarify upon this

Damien Watson
Damien Watson
27,419 Points

Hey, no problems. The question asks you to initialise it with the first four properties, not five. Then to define the description as a string representation of the passed in colours.

In this instance, there is no point passing in the description as it would be overwritten by the init definition.

Does this make sense?

Dhruv Mittal
Dhruv Mittal
932 Points

Yes, thank you so much.

I missed the "first four properties" part in the question

Damien Watson
Damien Watson
27,419 Points

Yeh, I know what you mean, so easy to do. :)