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

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
}
主站蜘蛛池模板: 当雄县| 肃南| 扎赉特旗| 师宗县| 东明县| 清丰县| 邢台市| 平利县| 日喀则市| 克山县| 密云县| 玉林市| 榆社县| 莲花县| 开阳县| 明光市| 措美县| 淮北市| 安溪县| 雅江县| 三原县| 梨树县| SHOW| 佳木斯市| 阿尔山市| 庆云县| 普陀区| 夏河县| 静安区| 莆田市| 加查县| 通海县| 仙游县| 兰州市| 新沂市| 秦皇岛市| 洞口县| 武城县| 巴中市| 同仁县| 库尔勒市|