- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 138字
- 2021-07-02 14:44:58
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.
推薦閱讀
- 計算機組成原理與接口技術:基于MIPS架構實驗教程(第2版)
- Python廣告數據挖掘與分析實戰
- “互聯網+”時代立體化計算機組
- Apache Kylin權威指南
- 云數據中心網絡與SDN:技術架構與實現
- 數據修復技術與典型實例實戰詳解(第2版)
- 中文版Access 2007實例與操作
- 數據庫與數據處理:Access 2010實現
- AndEngine for Android Game Development Cookbook
- 精通Neo4j
- SQL進階教程(第2版)
- Learn Selenium
- Creating Mobile Apps with Appcelerator Titanium
- 一本書讀懂區塊鏈(第2版)
- Access 2013 數據庫管理與應用從新手到高手