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

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
}
主站蜘蛛池模板: 临泉县| 赞皇县| 邵阳县| 武邑县| 招远市| 炉霍县| 乃东县| 晋宁县| 宣城市| 霍州市| 文水县| 梁河县| 永昌县| 沙洋县| 监利县| 清原| 房山区| 乐昌市| 新疆| 尖扎县| 建湖县| 平邑县| 米脂县| 保定市| 新巴尔虎右旗| 开阳县| 泽州县| 会东县| 吉木萨尔县| 兖州市| 长垣县| 安宁市| 华阴市| 营山县| 淅川县| 肇州县| 舒兰市| 绍兴市| 东安县| 兰溪市| 沙河市|