1

I create enum in blank swift file for manage color Schemes in my App with this block of code:

enum Color {
    case border
    case waterMelon
    case bleu
    case ufoGreen
    case lightBlue 
}

In the bottom of that I created a extension base on Color enum I just made.

Here the extension:

extension Color {
    var value: UIColor {
        var instanceColor = UIColor.clear

        switch self {
        case .border:
            instanceColor = UIColor(red:0.92, green:0.93, blue:0.94, alpha:1.00)
        case .waterMelon:
            instanceColor = UIColor(red:0.97, green:0.38, blue:0.45, alpha:1.00)
        default:
            instanceColor = UIColor.clear
        }

        return instanceColor

    }
}

Now the problem is when I wanna use those color I should used something like this:

//now : I don't like it.
view.backgroundView = Color.dark.value

//that how I want to be
view.backgroundView = Color.dark

// or like this
view.backgroundView = .dark

And I know it's because of the value that i declare on extension . but how can I get rid of that?

Dark star
  • 5,192
  • 9
  • 35
  • 54

1 Answers1

4

Don't use an enum then. If you don't want to enumerate on the values in a switch statement, there is no need for an enum. Use a struct with constant attributes.

struct Color {
    static let border = UIColor(red:0.92, green:0.93, blue:0.94, alpha:1.00)
    static let waterMelon = UIColor(red:0.97, green:0.38, blue:0.45, alpha:1.00)
    // and so on ....
}

If you want to extend UIColor to have access to all the other colors of UIColor as well, you can extend UIColor like this:

extension UIColor {
    static var border: UIColor {
        return UIColor(red:0.92, green:0.93, blue:0.94, alpha:1.00)
    }

    static var waterMelon: UIColor {
        return UIColor(red:0.97, green:0.38, blue:0.45, alpha:1.00)
    }
}
kiecodes
  • 1,642
  • 14
  • 28
  • But how about using like this: `.dark`? – Dark star Feb 05 '17 at 09:33
  • Oh I didn't know you want to have a extension of UIColor. EDITED. – kiecodes Feb 05 '17 at 09:36
  • 1
    In your first snippet above, you might want to let the immutable properties be of the type (`static`) rather of instances of it. – dfrib Feb 05 '17 at 09:50
  • Thank you @dfri! Good call. Edited. – kiecodes Feb 05 '17 at 10:22
  • 4
    You can still use an `enum` with `static` properties. In fact, a caseless `enum` is preferable over a `struct` as you cannot accidently initialise it. – Hamish Feb 05 '17 at 13:54
  • [A thread related to @Hamish:s comment](http://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum). – dfrib Feb 08 '17 at 17:09
  • 1
    There's actually an important reason why one might want to use enum cases instead of a struct. Type safety. If you want to create an UI component that can only have colors from a limited palette, you really would prefer using enum for that, because that immediately limits every user of that component to the palette that you want to stick to. – Lukas1 Jun 20 '17 at 06:36