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 Swift 2.0 Enumerations and Optionals Introduction to Enumerations Methods on Enumerations

Welby Obeng
Welby Obeng
20,340 Points

What exactly is happening with let redValue, let greenValue, let blueValue, let alphaValue?

What exactly is happening with let redValue, let greenValue, let blueValue, let alphaValue?

Why do we need to create a constant in function when we switch to .RGB? Havent we already assigned the value in case RGB(----)

import UIKit

enum ColorComponents {
    case RGB(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
    case HSB(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)

    func color() -> UIColor {
        switch self {
        case .RGB(let redValue, let greenValue, let blueValue, let alphaValue):
            return UIColor(red: redValue/255.0, green: greenValue/255.0, blue: blueValue/255.0, alpha: alphaValue)
        case .HSB(let hueValue, let saturationValue, let brightnessValue, let alphaValue):
            return UIColor(hue: hueValue/360.0, saturation: saturationValue/100.0, brightness: brightnessValue/100.0, alpha: alphaValue)
        }
    }
}

ColorComponents.RGB(red: 61.0, green: 120.0, blue: 198.0, alpha: 1.0).color()

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Welby Obeng,

You are correct. We did assign the red, green, blue, and alpha values for the members of the ColorComponents enumerations by using associated values. Now we want to access those values inside of the switch statement by extracting them with local constants.

I don't believe you can use the names provided to associated values in their member definitions. The compiler doesn't throw any errors, however, I haven't been able to use those names anywhere in my code. I think it may allow it solely for readability purposes.

Good Luck!