I would like to create a simple extension to UIColor such that I could do something like this:
extension UIColor {
enum colorString: String {
....
}
}
let array: [UIColor.colorString] = [ .red, .green, .blue]
let color: UIColor = array[1]
let text: String = array[1].rawValue
But I'm having trouble to make the extension part working. I've tried multiple attempts but not successful. I might have missed something. I would really appreciate your help.
You could do it like this. Depends on how many colors you want to do this for. Not sure what you intend to do with this but it doesn't look like the most useful extension.
extension UIColor {
convenience init(_ colorString: ColorString) {
switch colorString {
case .red:
self.init(red:1.0, green:0.0, blue:0.0, alpha:1.0)
case .green:
self.init(red:0.0, green:1.0, blue:0.0, alpha:1.0)
case .blue:
self.init(red:0.0, green:0.0, blue:1.0, alpha:1.0)
}
}
enum ColorString: String {
case red
case green
case blue
}
}
Your conversion from colorString to UIColor would have to be of the form
let color = UIColor(array[1])