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

Jonas Moltumyr
Jonas Moltumyr
1,939 Points

create a initalizer for rgbColor

Here´s the task:

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.

I dont quite understand what the task wants me to to with the constant description. Thought I should return the value as the description string, but I get som syntax errors it seems.

need some help on this one :) Thanks in advanced.

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 = red
        self.green = green
        self.blue = blue
        self.alpha = alpha



    }
    return description("red: \(red), green: \(green), blue: \(blue), alpha:\(alpha)")
}

5 Answers

Jonas Moltumyr
Jonas Moltumyr
1,939 Points

I understand what I did wrong...

here is the right code if anyone needs some help with this code challenger.

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

    }

}

Glad you got it fixed; I'll mark this as closed.

Steve.

Rich Braymiller
Rich Braymiller
7,119 Points

thank you, i had most of it, that last line just threw me off...difficult when they throw stuff in when its not even in the previous video...SMH

Hi, why would you use a set of parenthesis to interpolate description property's values ? This is all fine too : self.description = "red: \(colorRed), green: \(colorGreen), blue: \(colorBlue), alpha: \(opacity)"

How did you come across this post? This was made in Feb 2016 relating to a language version that is now long obsolete.

Did you have a question you wanted answering?

Steve.

Can someone explain the last line to me please?

Yes!

The init method needs to assign a value into all stored properties. The first 4 are all passed in as arguments, so red, green, blue and alpha are set by user input.

The last stored property, description, is made up of the other passed-in values. So the assignment into description is made up of a multiple interpolation of the other stored properties into a prescribed string.

Is that clear? If not, ask me a question and I'll answer it.

Steve.

Yes I got it thanks!

:+1:

Grayson Hary
Grayson Hary
5,033 Points

Steve, your explanation is very helpful!!!

Thank you!