枚举数据快速

我想使用类似 java的枚举,你可以在其中使用自定义数据的枚举实例.例如:
enum Country {
    case Moldova(capital: "Chișinău",flagColors: [Color.Blue,Color.Yellow,Color.Red]);
    case Botswana(capital: "Gaborone",Color.White,Color.Black]);
}

我后来写道:

Country.Moldova.capital;

似乎我可以指示变量,但不能指示值,我只能在使用枚举时指定值,而不是声明.哪种模仿这种行为最好?

你可以做这样的事情,这可能会有所帮助:(这只是一个非常通用的例子)
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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...