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 Complex Data Structures Custom Initializers

Mohib Shah
Mohib Shah
1,441 Points

My code won't compile but I don't seem to have any errors. Tried it in playground but it gave me a weird output

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

let description: String

init(red: Double, green: Double, alpha: Double, blue: Double) {
    self.red = red
    self.green = green
    self.alpha = alpha
    self.blue = blue

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

}

let myColors = RGBColor.init(red: 12.0, green: 13.0, alpha: 14.0, blue: 11.0)

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

    let description: String

    init(red: Double, green: Double, alpha: Double, blue: Double) {
        self.red = red
        self.green = green
        self.alpha = alpha
        self.blue = blue

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


}

let myColors = RGBColor.init(red: 12.0, green: 13.0, alpha: 14.0, blue: 11.0)

1 Answer

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

There is two things that make this not work on the code challenge. The first is the order, you have the blue parameter come before alpha. For the init method they must follow the same order as the constants. Second would be using the double values they give you in the directions. Alpha is usually always 1.0

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

    }


}

let description = RGBColor(red: 86.0, green: 22.4, blue: 184.2, alpha: 1.0)

Hope this helps!