Answer: Extension, in my professional opinion.
Think about it; you are, in philosophy, 'extending' the range of colors offered by UIColor
. Provided that your color name is distinct and the new function follows Apple's method naming protocol (i.e. Color
), extending UIColor
seems neater. One or two new colors (in my opinion) don't warrant an entire dedicated struct
.
Bonus answer:
Where would a struct
(or enum
!) be a good fit?
- If your app forces replacements for the standard colors (i.e. custom
'primary' colors)
- If your app is specifically designed to be themed / customized, it might be good have an
enum
, to serve as a concrete list for available options.
- If you can't think of standard names for the colors (
sharkBlueColor
, anyone?).
- If your app is specifically for drawing/painting (in which case a 'palette' construct might be good idea).
... the list goes on. You must learn to discern and decide for yourself as you mature as a Swift developer!