- 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.
推薦閱讀
- 同步:秩序如何從混沌中涌現
- 數據庫原理及應用教程(第4版)(微課版)
- SQL Server 2008數據庫應用技術(第二版)
- Voice Application Development for Android
- 卷積神經網絡的Python實現
- Access 2007數據庫應用上機指導與練習
- 商業分析思維與實踐:用數據分析解決商業問題
- UDK iOS Game Development Beginner's Guide
- WS-BPEL 2.0 Beginner's Guide
- 數據驅動設計:A/B測試提升用戶體驗
- Dependency Injection with AngularJS
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- 金融商業算法建模:基于Python和SAS
- 云原生數據中臺:架構、方法論與實踐
- Hadoop集群與安全