Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift 2.0 Complex Data Structures Custom Initializers

Swift - struct

struct RGBColor { let red: Double = 86.0 let green: Double = 191.0 let blue: Double = 131.0 let alpha: Double = 1.0

let description: String

// Add your code below

// 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

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

}

structs.swift
struct RGBColor {
    let red: Double = 86.0
    let green: Double = 191.0
    let blue: Double = 131.0
    let alpha: Double = 1.0

    let description: String

    // Add your code below

    // 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

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

4 Answers

Hi Marko,

You haven't actually asked a question, but I assume your having trouble with this challenge. There are a few problems that I see.

In your init you don't need to add description as a parameter as this value will be set by the init() method itself.

You do not need to set default values to red, green, blue or alpha.

You need to add alpha to your description output.

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

Hope this helps.

KB :octopus:

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You only have a few things wrong with your code.

  1. You shouldn't set values to the RGBColor properties inside the struct. Those values should only ever be set when you instantiate and RGBColor object.
  2. You shouldn't include description as a parameter in your init method. You set description using the red, green, blue, and alpha properties of RGBColor, so you don't need to input a description when initializing an RGBColor.
  3. You need to include alpha when you create description.
struct RGBColor {
    let red: Double = 86.0 // DON'T SET ANY VALUE TO red
    let green: Double = 191.0 // DON'T SET ANY VALUE TO green
    let blue: Double = 131.0 // DON'T SET ANY VALUE TO blue
    let alpha: Double = 1.0 // DON'T SET ANY VALUE TO alpha

    let description: String // THE ABOVE PROPERTIES SHOULD HAVE THE SAME FORMAT AS THIS PROPERTY

    // GET RID OF description FROM THE PARAMETERS OF THE INIT METHOD BELOW:
    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 = "red: \(self.red), green: \(self.green), blue: \(self.blue)" // ADD ALPHA TO THIS STRING
    }
}

I hope this helps!

Thanks for help. :)

Glenn Renwick
PLUS
Glenn Renwick
Courses Plus Student 2,114 Points

For the description String should you use:

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

or

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

Both seem to work but could anyone help explain which should be used and why?

Thanks