0

I was storing couple of UIColors inside an array and changed my code to get them as a return value of a function as an UIColor extension.

Array version:

var colors : [UIColor] = [UIColor(red:0.35, green:0.40, blue:0.45, alpha:1.00), UIColor(red:0.38, green:0.78, blue:0.56, alpha:1.00), UIColor(red:0.61, green:0.73, blue:0.38, alpha:1.00)]

Extension version:

extension UIColor{
    class func getColor(level: Double) -> UIColor{
        switch level{
        case 0:
            return UIColor(red:0.35, green:0.40, blue:0.45, alpha:1.00)
        case 1:
            return UIColor(red:0.38, green:0.78, blue:0.56, alpha:1.00)
        case 2:
            return UIColor(red:0.61, green:0.73, blue:0.38, alpha:1.00)
        default:
            return UIColor(red:0.35, green:0.40, blue:0.45, alpha:1.00)
        }
    }
}

So instead of colors[0] I can use UIColor.getColor(level: 0)

Is there any point in doing so and is it any better?

mikro098
  • 2,173
  • 2
  • 32
  • 48
  • Do the colors make sense being in an array? (i.e do you specifically need to access them by index?). If not, then using named static properties would be a better idea. Compare http://stackoverflow.com/q/42030338/2976878 – Hamish May 10 '17 at 15:48

2 Answers2

2

As another option, you could store them all as individual static values. UIColor already stores some colors in that way (for instance, UIColor.red is equivalent to UIColor(red: 1, green: 0, blue: 0, alpha: 1)), so doing it that way would be more consistent. That would give you this:

extension UIColor {
    static let color1 = (red:0.35, green:0.40, blue:0.45, alpha:1.00)
    static let color2 = (red:0.38, green:0.78, blue:0.56, alpha:1.00)
    static let color3 = (red:0.61, green:0.73, blue:0.38, alpha:1.00)
    static let color4 = (red:0.35, green:0.40, blue:0.45, alpha:1.00)
}
Vollan
  • 1,887
  • 11
  • 26
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
  • And how does it compare to enum (`enum Colors{ static let color1 = UIColor(red:0.35, green:0.40, blue:0.45, alpha:1.00)}`) and so on? – mikro098 May 10 '17 at 16:27
  • @codddeer123 Functionally, I don't think there's any difference. Personally I'd go with the extension for consistency, but it's basically a matter of personal preference. – John Montgomery May 10 '17 at 16:38
  • 1
    @codddeer123 Depends if the colors you're adding "feel like" they should be a part of the default color palette that `UIColor` gives you. If, for example, you're defining your own palette with different shades of red, blue and green to the `UIColor` defaults – then IMO an `enum` with static properties is more appropriate (rather than adding `customRed`, `customBlue` properties in a `UIColor` extension). – Hamish May 10 '17 at 17:00
  • So in general using extension like this is better than the array? – mikro098 May 10 '17 at 17:20
  • @codddeer123 It depends on the context, particularly whether it makes sense for the items to be indexed. In this particular case, I think static values make more sense and allow your references to be more descriptive, unless for some reason you *really need* to be able to reference them by an index number. – John Montgomery May 10 '17 at 17:26
1

One possible benefit for the extension is a wider use across multiple classes. But you could also achieve the same result if you created a Singleton for constants. One problem is that it is much easier to remove and add colors from your array and you can do that at runtime. If you hard-code it into an extension you won't be able to modify that during runtime.

I'd say if you have multiple constants like your color array, then you could go ahead and use a Singleton class for that purpose.

If you have a huge amount of colors and that's all you will be needing to store, the extension works fine.

If you have just a small number of colors like the list in your question or if you know you will be needing to modify the list, then an array would be just fine for that.

LuKenneth
  • 2,197
  • 2
  • 18
  • 41