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

Gabriel Ducharme
Gabriel Ducharme
11,948 Points

Custom initializer method in swift.

Why do I keep getting the error message to create my own custom initializer in the test? Isn't it what I'm doing?

struct RGBColor {

    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double

    let description: String


    // Add your code below

    init(r: Double, g: Double, b: Double, a: Double, description: String) {
        self.red = r
        self.green = g
        self.blue = b
        self.alpha = a
        self.description = "red: \(r), green: \(g), blue: \(b), alpha \(a)"
    }

}

1 Answer

As you don't use the description parameter for creating a value for the description property, it is not necessary to declare it within the list of initializer parameters. The signature of the initializer should probably look like this:

init(r: Double, g: Double, b: Double, a: Double) {
    // Your initialization code goes here...
}