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

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.

主站蜘蛛池模板: 荔浦县| 乐陵市| 紫云| 赤城县| 边坝县| 屯昌县| 固阳县| 黄梅县| 万全县| 新昌县| 马尔康县| 揭阳市| 台南县| 罗江县| 若羌县| 新龙县| 铜山县| 鄢陵县| 水富县| 中卫市| 珲春市| 肇庆市| 遵义县| 榕江县| 库尔勒市| 大英县| 香格里拉县| 无为县| 萍乡市| 孟州市| 彭山县| 临澧县| 含山县| 泊头市| 漾濞| 宁夏| 灵寿县| 会东县| 集安市| 酒泉市| 孝义市|