0

I'm using an Util class in swift as helper class. Besides functions I want to implement some constants with custom colors.

It's correct to use a Struct in this way?

class Util: NSObject {

struct Colors {
    static let white = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
    static let orangeCantaloupe = UIColor(red: 1, green: 204/255, blue: 102/255, alpha: 1)
    static let greyMercury = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
    static let greyMagnesium = UIColor(red: 179/255, green: 179/255, blue: 179/255, alpha: 1)

}

class func anyFunction() {

.......
 }
}
bullC
  • 58
  • 1
  • 6

1 Answers1

1

You can extend UIColor with new colours:

extension UIColor {
   static let white = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
   static let orangeCantaloupe = UIColor(red: 1, green: 204/255, blue: 102/255, alpha: 1)
   static let greyMercury = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
   static let greyMagnesium = UIColor(red: 179/255, green: 179/255, blue: 179/255, alpha: 1)
}

In contexts where a UIColor is expected, the type can be implied, so you can write just something.color = .orangeCantaloupe

Alternatively, you can keep them in a seperate name space (for clarity over convenience), like BrandColors. An empty enum works best, so you know that nobody will accidentally instantiate a senseless object.

Alexander
  • 59,041
  • 12
  • 98
  • 151