【问题标题】:Swift enum function with string parameter calling with a parameter of the enum class使用枚举类的参数调用字符串参数的 Swift 枚举函数
【发布时间】:2018-08-22 03:18:58
【问题描述】:

我正在尝试将字符串转换为枚举键值,但找不到任何简单的方法,因此我对其进行了硬编码,但它仍然无法正常工作。

我尝试使用枚举函数从字符串中返回枚举键值,但我无法使用字符串调用它,即使我使用字符串作为参数声明它。

然后我试图将它移到另一个班级,但同样的事情发生了。

我的相关代码如下。

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
    }
}

{

当我尝试调用它时显示的代码是 this.Picture1

手动完成时,仍然显示cannot convert string to pickColor的错误。

然后我将代码移到了一个新类中,但是它仍然不起作用。

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

发生了什么事?

【问题讨论】:

  • 你应该使用switch而不是if/else-if/else阶梯

标签: ios swift xcode parameters enums


【解决方案1】:

您需要将 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
}

【讨论】:

  • @LeoDabus 除了对 OP 的代码进行最小的更改来解释它为什么不起作用之外,没有真正的充分理由。
【解决方案2】:

在过去的几年里,我尝试了很多这样的变体,并最终确定了以下内容。它类似于@rmaddy 的版本,但枚举的 rawValue 是一个字符串,因此很容易在名称和枚举之间来回转换。

然后我为实际颜色本身设置了静态变量。 这允许我以类似于 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's,您不需要执行 if let,因为大多数 UIColor 属性也会取零

【讨论】:

    【解决方案3】:

    添加计算属性和开关以将枚举案例转换为六进制并使用默认枚举 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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多