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

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.

主站蜘蛛池模板: 垣曲县| 昌吉市| 普陀区| 岳普湖县| 鹤山市| 河源市| 河源市| 从化市| 太湖县| 安图县| 乌鲁木齐县| 苏尼特右旗| 兴安盟| 通海县| 奉节县| 宿州市| 新田县| 大兴区| 兴海县| 丁青县| 东方市| 垦利县| 墨玉县| 南投市| 金阳县| 芜湖县| 邯郸县| 孝昌县| 龙井市| 白沙| 海南省| 商都县| 达州市| 南漳县| 澜沧| 枣强县| 台前县| 县级市| 灯塔市| 新民市| 安溪县|