- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 169字
- 2021-07-02 14:44:58
Raw type enums
A raw type is a base type for all enumeration members; in our example, we can hardcode presets for our dimming, as follows:
enum LightLevel: String {
case quarter
case half
case threequarters
}
let state: State<LightLevel> = .dimmed(.half)
Thanks to the generic implementation and the fact that String is equatable, we can use this raw value in our dimmed state.
With the LightLevel enum, which has a raw type of String, the compiler will use the member name as the underlying raw value:
LightLevel.half.rawValue == “half” // == true
You can override these by specifying them, as follows:
enum LightLevel: String {
case quarter = “1/4”
case half = “1/2”
case threequarters = “3/4”
}
When using Int as a raw type, the underlying raw values will follow the order of the cases:
enum Level: Int {
case base // == 0
case more // == 1
case high = 100
case higher // == 101
}
推薦閱讀
- 我們都是數據控:用大數據改變商業、生活和思維方式
- ETL數據整合與處理(Kettle)
- SQL查詢:從入門到實踐(第4版)
- Spark核心技術與高級應用
- 大數據Hadoop 3.X分布式處理實戰
- 數據驅動設計:A/B測試提升用戶體驗
- Sybase數據庫在UNIX、Windows上的實施和管理
- 數據庫技術及應用教程
- 一個64位操作系統的設計與實現
- 數據庫設計與應用(SQL Server 2014)(第二版)
- INSTANT Android Fragmentation Management How-to
- 計算機組裝與維護(微課版)
- 碼上行動:利用Python與ChatGPT高效搞定Excel數據分析
- 推薦系統全鏈路設計:原理解讀與業務實踐
- Access 2010數據庫應用技術教程(第二版)