枚举数据快速

时间:2014-07-09 08:25:13

标签: enums swift

我想使用类似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 个答案:

答案 0 :(得分:11)

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

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

答案 1 :(得分:2)

不幸的是,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])
)

答案 2 :(得分:2)

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

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
相关问题