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

Vera Hsu
Vera Hsu
997 Points

Having troubles with initializer

I don't know what is wrong with my code. Bummer said "Don't use the member wise initializer Swift creates. Add your own custom initializer as specified in the directions" which I don't quit get its meaning. Any help would be great. Thanks.

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

}

2 Answers

Ah, yes, but you do initialize description - you just don't pass a value in. The constructor will assign a value to description - just like you have already with this:

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

But to form that string you don't need to pass a value in for description, it is made up of the values passed in for the other four properties. So, the compiler will be happy; description holds a value that is formed by the other four values that are passed into the constructor.

You don't need to pass values to a constructor; you can assign values to properties. In your existing code, you have assigned values to all five stored properties without using any of the values you passed in. So, this is valid:

    init () {
        self.red = 86.0
        self.green = 191.0
        self.blue = 131.0
        self.alpha = 1.0
        self.description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
    }

No parameters but all stored properties are initialized.

What we need here is a middle ground - pass in some values but not all:

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

OK with that?

Steve.

Vera Hsu
Vera Hsu
997 Points

I understood. This part is a little tricky and hard for me to understand haha. Huge thanks to you, Steve. Thanks for your explicit explanation!!

This one is a difficult challenge, yes! I'm glad you understand it and got through. :smile: :+1:

Hi Vera,

You don't need to pass description in as a parameter to the contructor. The constructor itself generates the content that is stored in the description property. You don't have a value for it so can't pass it in.

Also, the values for the other elements are given by way of example; don't hard code those inside the constructor. So, each RGBA value will be passed into the contructor as the parameters, then assigned to the stored properties inside the method. You've got red: Double being passed into the constructor; inside it you will then say that self.red = red to assign the value sent in; not the hard-coded example figures.

Does that make sense?

Let me know how you get on.

Steve.

Vera Hsu
Vera Hsu
997 Points

But if I don't initialize description, there will be a compile error about "didn't initialize all the stored properties..." somethings like that. What do I do with that?? thanks