枚举数据迅速

编程入门 行业动态 更新时间:2024-10-14 08:30:11
本文介绍了枚举数据迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I would like to use java-like enums, where you can have enum instances with custom data. For instance:

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;

似乎我可以指示变量,而不是值,我只能分配值当使用枚举时,不声明。这将是最好的模拟这种行为的方式?

It seems that I can indicate the variables, but not the values, and I can only assign the values when using the enum, not declaring. Which would be the best way to mimic this behaviour?

推荐答案

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

you can do something like this, which may be helpful: (that is a very generic example only)

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() } } }

那么你可以访问这样的细节:

then you can access to the details like this:

let country: Country = Country.Botwana

获取资本

这样:

get the capital

that way:

let capital: String = country.capital()

或另一个:

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

或第三个:

let capital: String = country.capitolName

获取标志的颜色:

get the flag's colours:

that way:

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

或另一个:

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

或第三个:

let flagColours: Array<UIColor> = country.flagColoursArray

更多推荐

枚举数据迅速

本文发布于:2023-11-26 20:39:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!