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

Associating values

Enums can also contain associated values. In our scenario, we can leverage this to represent a dimmer. A dimmer changes the intensity of the light, so we can represent it with a third member-the dimmed member:

enum State: Equatable {
case on
case off
case dimmed(value: Double)
}

You may have noticed that we needed to add the Equatable conformance. This is required, as otherwise, the compiler can't synthesize equality anymore without our hint. This implementation works, but we lack a few things. First, not all Double values are valid; we'd probably like to keep these in a reasonable span (between 0 and 1, for example). But perhaps not all of our lights support such values between 0 and 1. Others may want to support between 0 and a 100 or integers between 0 and 255.

主站蜘蛛池模板: 铜梁县| 冷水江市| 福州市| 文登市| 海伦市| 泽州县| 曲周县| 云龙县| 宁波市| 太仓市| 思茅市| 武陟县| 鸡泽县| 都昌县| 盐池县| 东山县| 屏山县| 怀仁县| 乳山市| 山丹县| 铜川市| 方山县| 辉县市| 横山县| 葫芦岛市| 东源县| 渭源县| 新巴尔虎右旗| 宜宾市| 天津市| 五河县| 泰宁县| 龙泉市| 西充县| 洪湖市| 东辽县| 张家港市| 兴山县| 绥芬河市| 肇州县| 武定县|