- 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.
- FreeSWITCH 1.8
- MySQL數據庫應用與管理 第2版
- Debian 7:System Administration Best Practices
- SQL學習指南(第3版)
- Java Web基礎與實例教程(第2版·微課版)
- ASP.NET Core Essentials
- Visual C++數字圖像模式識別技術詳解
- Java編程指南:基礎知識、類庫應用及案例設計
- 大學計算機基礎(第2版)(微課版)
- C語言實驗指導及習題解析
- 量化金融R語言高級教程
- Unity 2D Game Development Cookbook
- Bootstrap 4 Cookbook
- The Professional ScrumMaster’s Handbook
- Java Web開發就該這樣學