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

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,704 Points

I don't understand "let redValue, let greenValue..." here

Full code:

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

I don't understand this line:

case .RGB(let redValue, let greenValue, let blueValue, let alphaValue):

What's happening here? Are we specifying parameter names? Why do we need to use "let"?

1 Answer

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi Brendan

As I understand it, the constants just represent a given value of RGB (red, green and blue + alpha). So like I commented in the below code, we could have written any actual value, but since we are writing a method that has to be flexible to all colors, we use constants to store the values. (Not sure if that's the "correct" explanation, but that's just how I understand it..)

    func color() -> UIColor {
        switch self {
        case .RGB(
            let redValue,
            let greenValue,
            let blueValue,
            let alphaValue): return UIColor(
                red: redValue/255.0, //this is the same as: red: 234/255.0
                green: greenValue/255.0, //this is the same as: green: 62/255.0
                blue: blueValue/255.0, //this is the same as: blue: 23/255.0
                alpha: alphaValue //this is the same as: alpha: 1.0
                )
            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
                    )
        }
    }
}
Marina Alenskaja
Marina Alenskaja
9,320 Points

I'm pretty sure they are temporary stored properties - Pasan mentions this in some videos.

Hmmm.. Close but not exactly. The constant names are more or less for convenience and readability (most of all readability). Stuff like this becomes more and more essential the more developers you have touching the same codebase as you. The more readable the code, the more maintainable it is. But most importantly, in this case, we're passing those constants to the UIColor init method in our switch statement. Pasan is basically showing you how flexible/powerful enums are.