- Swift 4 Programming Cookbook
- Keith Moon
- 167字
- 2021-07-08 10:21:26
Enumerations
Enumerations are a programming construct that let you define a value type with a finite set of options. Most languages have enumerations (usually abbreviated to enums), including C and, by extension, Objective-C.
An example of an enum from the iOS/macOS SDK is NSComparisonResult, which you would use when sorting items. When comparing for the purposes of sorting, there are only three possible results from a comparison:
- ascending : The items are ordered in ascending order
- descending : The items are ordered in descending order
- same : The items are the same
There are a finite number of possible options for a comparison result; therefore, it's a perfect candidate for being represented by an enum:
enum ComparisonResult : Int {
case orderedAscending
case orderedSame
case orderedDescending
}
Swift takes the enum concept and elevates it to a first class type. As we will see, this makes enums a very powerful tool for modeling your information.
This recipe will examine how and when to use enums in Swift.
- Vue.js 3.x快速入門
- C語言程序設計實踐教程(第2版)
- PWA入門與實踐
- HTML5 移動Web開發從入門到精通(微課精編版)
- Vue.js快速入門與深入實戰
- 神經網絡編程實戰:Java語言實現(原書第2版)
- GitLab Repository Management
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- 精通Python自然語言處理
- 單片機C語言程序設計實訓100例
- Express Web Application Development
- Learning Concurrency in Kotlin
- Python全棧數據工程師養成攻略(視頻講解版)
- Python 3快速入門與實戰
- Mastering OpenStack