【问题标题】:Enums with data in swift快速枚举数据
【发布时间】:2014-07-09 08:25:12
【问题描述】:

我想使用类似 java 的枚举,您可以在其中拥有带有自定义数据的枚举实例。例如:

enum Country {
    case Moldova(capital: "Chișinău", flagColors: [Color.Blue, Color.Yellow, Color.Red]);
    case Botswana(capital: "Gaborone", flagColors: [Color.Blue, Color.White, Color.Black]);
}

我可以稍后再写:

Country.Moldova.capital;

看来我可以表示变量,但不能表示值,而且我只能在使用枚举时赋值,而不是声明。模仿这种行为的最佳方式是什么?

【问题讨论】:

    标签: enums swift


    【解决方案1】:

    您可以这样做,这可能会有所帮助:(这只是一个非常通用的示例)

    enum Country : Int {
        case Moldova, Botwana;
    
        //
    
        func capital() -> String {
            switch (self) {
            case .Moldova:
                return "Chișinău"
            case .Botwana:
                return "Gaborone"
            default:
                return ""
            }
        }
    
        //
    
        func flagColours() -> Array<UIColor> {
            switch (self) {
            case .Moldova:
                return [UIColor.blueColor(), UIColor.yellowColor(), UIColor.redColor()]
            case .Botwana:
                return [UIColor.blueColor(), UIColor.whiteColor(), UIColor.blackColor()]
            default:
                return []
            }
        }
    
        //
    
        func all() -> (capital: String, flagColours: Array<UIColor>) {
            return (capital(), flagColours())
        }
    
        //
    
        var capitolName: String {
        get {
            return capital()
        }
        }
    
        //
    
        var flagColoursArray: Array<UIColor> {
        get {
            return flagColours()
        }
        }
    
    }
    

    那么您可以像这样访问详细信息:

    let country: Country = Country.Botwana
    

    获取资本

    那样:

    let capital: String = country.capital()
    

    或其他:

    let capital: String = country.all().capital
    

    或第三个:

    let capital: String = country.capitolName
    

    获取标志的颜色

    那样:

    let flagColours: Array<UIColor> = country.flagColours()
    

    或其他:

    let flagColours: Array<UIColor> = country.all().flagColours
    

    或第三个:

    let flagColours: Array<UIColor> = country.flagColoursArray
    

    【讨论】:

    • 这就是我最终做的,虽然我不认为这是理想的,但它确实模仿了我正在寻找的行为。
    【解决方案2】:

    这是另一个通用示例,与holex 发布的示例类似,但更接近Java 中的外观。 (我喜欢它,因为所有自定义数据都在一个地方)。我没有在单独的方法中打开“self”,而是简单地创建一个静态字典,将每个案例映射到一个包含适当数据的元组。然后,我有方便的 var 来轻松获取数据。

    enum TestEnum {
        case One
        case Two
        case Three
    
        private static let associatedValues = [
            One:    (int: 1, double: 1.0, string: "One"),
            Two:    (int: 2, double: 2.0, string: "Two"),
            Three:  (int: 3, double: 3.0, string: "Three")
        ]
    
        var int: Int {
            return TestEnum.associatedValues[self]!.int;
        }
    
        var double: Double {
            return TestEnum.associatedValues[self]!.double;
        }
    
        var string: String {
            return TestEnum.associatedValues[self]!.string;
        }
    }
    

    您可以像这样访问自定义数据:

    println(TestEnum.One.int)      // 1
    println(TestEnum.Two.double)   // 2.0
    println(TestEnum.Three.string) // Three
    

    【讨论】:

    • 我真的很喜欢这个解决方案,因为它将每个枚举数据保存在一行中。你知道如何让编译器检查associatedValues中是否涵盖了所有案例吗?
    【解决方案3】:

    不幸的是,raw values 的枚举似乎仅限于文字值。你可能想file a bug

    作为替代方案,您可以执行以下操作:

    let Country = (
        Moldova: (capital: "Chișinău", flagColors: [Color.Blue, Color.Yellow, Color.Red]),
        Botswana: (capital: "Gaborone", flagColors: [Color.Blue, Color.White, Color.Black])
    )
    

    或者这个:

    struct Country {
        let capital: String
        let flagColors: [Color]
    }
    
    let Countries = (
        Moldova: Country(capital: "Chișinău", flagColors: [.Blue, .Yellow, .Red]),
        Botswana: Country(capital: "Gaborone", flagColors: [.Blue, .White, .Black])
    )
    

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 2020-05-19
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多