枚举数据快速
发布时间:2020-12-14 05:26:07 所属栏目:百科 来源:网络整理
导读:我想使用类似 java的枚举,你可以在其中使用自定义数据的枚举实例.例如: enum Country { case Moldova(capital: "Chi?in?u",flagColors: [Color.Blue,Color.Yellow,Color.Red]); case Botswana(capital: "Gaborone",Color.White,Color.Black]);} 我后来写道
我想使用类似
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |