我正在尝试从字符串转换为枚举键值,但是我找不到任何简单的方法,因此我对其进行了硬编码,但仍然无法正常工作。
我尝试使用枚举函数从字符串返回枚举键值,但是即使我使用String作为参数声明了它,也无法使用字符串调用它。
然后我尝试将其移至另一个类,但发生了相同的事情。
我的相关代码如下。
enum pickedColor: String {
case green = "71D25E"
case red = "FF0000"
case maroon = "800000"
case yellow = "FFFF00"
case olive = "808000"
case lime = "00FF00"
case aqua = "00FFFF"
case teal = "008080"
case blue = "0000FF"
case navy = "000080"
case fuchsia = "FF00FF"
case purple = "800080"
func toEnum(_ colorName: String) -> pickedColor {
if colorName.elementsEqual("green") {
return .green
} else if colorName.elementsEqual("red") {
return .red
} else if colorName.elementsEqual("maroon") {
return .maroon
} else if colorName.elementsEqual("yellow") {
return .yellow
} else if colorName.elementsEqual("olive") {
return .olive
} else if colorName.elementsEqual("lime") {
return .lime
} else if colorName.elementsEqual("aqua") {
return .aqua
} else if colorName.elementsEqual("teal") {
return .teal
} else if colorName.elementsEqual("blue") {
return .blue
} else if colorName.elementsEqual("navy") {
return .navy
} else if colorName.elementsEqual("fuchsia") {
return .fuchsia
} else {
return .purple
}
}
{
当我尝试调用它时显示的代码是这个。Picture1
手动完成后,仍然显示无法将字符串转换为pickedColor的错误。
然后我将代码移至新类,但是,它仍然无法正常工作。
class Color {
func toEnum(_ colorName: String) -> pickedColor {
if colorName.elementsEqual("green") {
return .green
} else if colorName.elementsEqual("red") {
return .red
} else if colorName.elementsEqual("maroon") {
return .maroon
} else if colorName.elementsEqual("yellow") {
return .yellow
} else if colorName.elementsEqual("olive") {
return .olive
} else if colorName.elementsEqual("lime") {
return .lime
} else if colorName.elementsEqual("aqua") {
return .aqua
} else if colorName.elementsEqual("teal") {
return .teal
} else if colorName.elementsEqual("blue") {
return .blue
} else if colorName.elementsEqual("navy") {
return .navy
} else if colorName.elementsEqual("fuchsia") {
return .fuchsia
} else {
return .purple
}
}
}
错误参数的第二张图片在这里。 Picture2
这是怎么回事?
答案 0 :(得分:2)
您需要将toEnum
函数设为静态函数,因为您没有在特定的枚举实例上调用它。
您还应该以大写字母开头来命名枚举。
switch
比您的长if/else
好。我也会考虑处理一种未知的颜色。
enum PickedColor: String {
case green = "71D25E"
case red = "FF0000"
case maroon = "800000"
case yellow = "FFFF00"
case olive = "808000"
case lime = "00FF00"
case aqua = "00FFFF"
case teal = "008080"
case blue = "0000FF"
case navy = "000080"
case fuchsia = "FF00FF"
case purple = "800080"
static func toEnum(_ colorName: String) -> PickedColor? {
switch colorName {
case "green":
return .green
case "red":
return .red
case "maroon":
return .maroon
case "yellow":
return .yellow
case "olive":
return .olive
case "lime":
return .lime
case "aqua":
return .aqua
case "teal":
return .teal
case "blue":
return .blue
case "navy":
return .navy
case "fuchsia":
return .fuchsia
case "purple":
return .purple
default:
return nil
}
}
}
现在您可以像尝试一样调用它:
if let color = PickedColor.toEnum(colorName) {
// use color as needed
}
答案 1 :(得分:1)
添加计算属性和开关以将枚举用例转换为hexa并使用默认枚举rawValue初始值设定项要容易得多:
enum PickedColor: String {
case green, red, maroon, yellow, olive, lime, aqua, teal, blue, navy, fuchsia, purple
}
extension PickedColor {
var hexa: String {
let hexa: String
switch self {
case .green:
hexa = "71D25E"
case .red:
hexa = "FF0000"
case .maroon:
hexa = "800000"
case .yellow:
hexa = "FFFF00"
case .olive:
hexa = "808000"
case .lime:
hexa = "00FF00"
case .aqua:
hexa = "00FFFF"
case .teal:
hexa = "008080"
case .blue:
hexa = "0000FF"
case .navy:
hexa = "000080"
case .fuchsia:
hexa = "FF00FF"
case .purple:
hexa = "800080"
}
return hexa
}
}
if let color = PickedColor(rawValue: "purple") {
print(color) // "purple\n"
print(color.hexa) // "800080"
}
答案 2 :(得分:1)
在过去的几年中,我尝试了许多这样做的变体,并选择了以下方法。它类似于@rmaddy的版本,但是枚举的rawValue是一个String,因此可以轻松在名称和枚举之间来回转换。
然后我将静态变量用于实际的颜色本身。 这使我可以以类似于UIColor的方式引用颜色
view.backgroundColor = Palette.blueColor
或
view.backgroundColor = Palette.colorNamed("blue")
。
enum Palette : String
{
case white
case orange
case red
case pink
case purple
case blue
static func color( named:String) -> UIColor?
{
switch named
{
case white.rawValue : return whiteColor
case orange.rawValue : return orangeColor
case red.rawValue : return redColor
case pink.rawValue : return pinkColor
case purple.rawValue : return purpleColor
case blue.rawValue : return blueColor
default : return nil
}
}
static let whiteColor = color( withHex:0xffffff)
static let orangeColor = color( withHex:0xfc622f)
static let redColor = color( withHex:0xdd202b)
static let pinkColor = color( withHex:0xff2f7e)
static let purpleColor = color( withHex:0x9166e6)
static let blueColor = color( withHex:0x049edd)
}
extension UIColor
{
convenience init( withHexAlpha hex:UInt32)
{
let red = CGFloat((hex >> 24) & 0xff) / 255.0
let green = CGFloat((hex >> 16) & 0xff) / 255.0
let blue = CGFloat((hex >> 8) & 0xff) / 255.0
let alpha = CGFloat((hex >> 0) & 0xff) / 255.0
self.init( red:red, green:green, blue:blue, alpha:alpha)
}
convenience init( withHex hex:UInt32)
{
self.init( withHexAlpha:(hex << 8) | 0xff)
}
}
使用我的和@rmaddy的您都不需要执行if let
,因为大多数UIColor属性也将花费nil