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 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!