Swift中带有数据的枚举

8
我想使用类似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;

看起来我可以指定变量,但不能指定值,并且只能在使用枚举时分配值,而不能在声明时赋值。哪种方法最好模拟这种行为?

3个回答

16

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

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

这就是我最终做的事情,虽然我认为它并不理想,但它确实模拟了我所寻找的行为。 - André Fratelli

3

这是一个与holex发布的示例相似,但更接近Java的通用示例。 (我喜欢它,因为所有自定义数据都在一个地方)。 我不是在不同的方法中切换“self”,而是创建了一个静态字典,将每个情况映射到包含适当数据的元组。 然后,我有方便的变量轻松获取数据。

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中是否覆盖了所有情况? - Andres Riofrio

2

很不幸,似乎带有原始值的枚举类型只支持字面量。您可能需要提交错误报告

作为替代方案,您可以尝试以下方法:

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])
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接