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

Unsure why this is not creating a custom initializer

I've been working on this for a while and just can't figure it out. The exercise is to create a customer initializer, but I keep failing and getting a message that I'm not using a custom initializer. Can someone help me figure out what I'm doing wrong?

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

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Since you are hard coding the values for the description property, then you don't have to add "description" to your parameters..

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

also, the "self.description" is optional at this point since the compiler knows what you are referring to. You only use "self", when there is a naming conflict, and the compiler does not know what you are referring to.

For example

struct AnyStruct{

var name: String

    init(name: String) {
        self.name = name 
     }
}

In this example, "self.name" refers to the store property var name: String, and "name" is the parameter, init(name: String).

Hope this helps you a bit. Good luck

Thanks for that! Makes sense now