Enumerations are a programming construct that lets you define a value type with a finite set of options. Most programming languages have enumerations (usually abbreviated to enums), although the Swift language takes the concept further than most.
An example of an enum from the iOS/macOS SDK is ComparisonResult, 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...