Enumerations
Enumerations (also known as enums) are a special data type that enables us to group related types together and use them in a type-safe manner. Enumerations in Swift are not tied to integer values as they are in other languages, such as C or Java. In Swift, we are able to define an enumeration with a type (string, character, integer, or floating-point value) and then define its actual value (known as the raw value). Enumerations also support features that are traditionally only supported by classes, such as computed properties and instance methods. We will discuss these advanced features in depth in Chapter 7, Classes, Structures, and Protocols. In this section, we will look at the traditional features of enumerations.
We will define an enumeration that contains a list of Planets
, like this:
enum Planets {
case mercury
case venus
case earth
case mars
case Jupiter
case Saturn
case Uranus
case neptune
}
Note: When defining...