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

oliverchou
oliverchou
20,886 Points

I couldn't figure out what's going on! Anyone help me?

I cannot distinguish between "self" properties and others. And I don't know wheher to put the "discription assinment" statement , in the "init" or create another "method" or simply inside the "struc"?

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

        self.discription = "red: " + red + ",green: " + green + ",blue: " + blue + ",alpha: " + alpha
    }
}

2 Answers

Hi there,

A couple of things - first, you don't want to pass the description in as a parameter to the init method. You create description within init. Second, spell description correctly - that'll stop confusion between the top of the struct and your init method.

Lastly, maybe consider using interpolation to construct your description string. That all looks like:

    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)"
    }

I hope that helps.

Steve.

No problem! :+1:

Steve, your answer to this question just helped me complete the code challenge, so thank you! I was running into issues - I thought I had tried everything - until i stopped including description as a parameter.

My question is, why do we create the description inside the init rather than passing it as a parameter?

Hi Corey,

I'm glad my answer helped! :+1:

There is no need to pass in description as a parameter as it is made up of the other parameters. To pass it in would duplicate typing without adding anything.

Does that make sense?

Steve.

Ahhh, I see. That makes perfect sense when you explain it that way. Thank you Steve!

No problem! :smile: