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

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
}
主站蜘蛛池模板: 湖北省| 井冈山市| 南乐县| 马边| 白城市| 慈利县| 天全县| 普宁市| 新郑市| 永安市| 汝南县| 额尔古纳市| 民权县| 星子县| 泰兴市| 东台市| 准格尔旗| 玛纳斯县| 舒城县| 博湖县| 宁远县| 高平市| 钟山县| 临漳县| 常宁市| 荃湾区| 上饶市| 中宁县| 金山区| 延安市| 东乌珠穆沁旗| 临夏县| 大姚县| 成武县| 汉中市| 彩票| 瑞昌市| 高唐县| 汉川市| 雷州市| 南江县|