官术网_书友最值得收藏!

Raw type enums

A raw type is a base type for all enumeration members; in our example, we can hardcode presets for our dimming, as follows:

enum LightLevel: String {
case quarter
case half
case threequarters
}

let state: State<LightLevel> = .dimmed(.half)

Thanks to the generic implementation and the fact that String is equatable, we can use this raw value in our dimmed state.

With the LightLevel enum, which has a raw type of String, the compiler will use the member name as the underlying raw value:

LightLevel.half.rawValue == “half” // == true

You can override these by specifying them, as follows:

enum LightLevel: String {
case quarter = “1/4”
case half = “1/2”
case threequarters = “3/4”
}

When using Int as a raw type, the underlying raw values will follow the order of the cases:

enum Level: Int {
case base // == 0
case more // == 1
case high = 100
case higher // == 101
}
主站蜘蛛池模板: 景泰县| 城口县| 阿尔山市| 漳平市| 诸暨市| 咸宁市| 克东县| 九龙城区| 曲阜市| 临沧市| 平舆县| 卢湾区| 伊宁市| 平湖市| 清镇市| 丰县| 呼伦贝尔市| 板桥市| 兴安盟| 弋阳县| 垫江县| 白朗县| 新沂市| 桑日县| 特克斯县| 永和县| 定南县| 庐江县| 岱山县| 柘荣县| 平湖市| 德庆县| 泾阳县| 二连浩特市| 芜湖县| 彭泽县| 大连市| 九龙县| 蓝山县| 上饶县| 泽库县|