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

Von Chucwuemeca
Von Chucwuemeca
4,746 Points

Initializers - I've reviewed my code and it should work, however I may be missing something.

The playground informs me that I should not use memberwise initializers, however I've used custom variables for the initializer.

structs.swift
struct RGBColor {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double

    let description: String

       // Add your code below

    init(colorRed: Double = 86.0, colorGreen: Double = 191.0, colorBlue: Double = 131.0, alphaColor: Double = 1.0, theDescription: String = "green") {
        self.red = colorRed
        self.green = colorGreen
        self.blue = colorBlue
        self.alpha = alphaColor
        self.description = "red: \(colorRed), green: \(colorGreen) blue: \(colorBlue) alpha: \(alphaColor)"
    }


}

2 Answers

Although this code is right in it's own way, the question doesn't ask for it. It is clearly given in the question that you should declare a custom initializer rather than using the default initializer provided by Swift.

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.

According to the question, the code can be modified as below-

struct RGBColor {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double

    let description: String

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

    }
}

Hope this helps :)

Von Chucwuemeca
Von Chucwuemeca
4,746 Points

Ok, I thought writing my own customer initializers meant using custom terms. I now understand, thanks.