我尝试使用UIColor作为枚举类型的原始值进行声明。以下是代码:
enum SGColor: UIColor {
case red = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
case green = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
case purple = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
}
但是我在第一行遇到了两个错误:
如果我采用第一种建议,Xcode将在括号内部的开头添加
'SGColor' declares raw type 'UIColor', but does not conform to RawRepresentable and conformance could not be synthesized
Do you want to add protocol stubs? Fix it
Raw type 'UIColor' is not expressible by any literal
typealias RawValue = <#type#>
。但我不确定该怎么做。
如果我要解决第二个错误,我该如何将原始类型更改为文字?
SGColor
、SGShading
等)。而且,一些答案对UIColor
类进行修改,例如添加扩展,这对我来说听起来并不是很安全。无论如何,感谢你提供的链接! - jackxujh