Alex headshot

AlBlue’s Blog

Macs, Modularity and More

Swift - enums

2014 Swift Mac

In this episode of the Swift introduction series we’ll be looking at enums in Swift.

Enumerations (aka enums) are found in most programming languages as a means to associate a symbol with set membership over a closed group of alternatives. In some cases, the values associated with an enum are meaningless (e.g. Male, Female) and in other cases they hold useful data (e.g. East might have a value of 90 and NorthEast might have a value of 45).

Traditional languages represented an enumeration always as an alias for an integral value and used sleight-of-hand to replace the value at compile time, like a set of #define constants. Although this worked, it meant that enums could be substituted for one another even when it didn’t make sense (e.g. Male == North).

Swift’s enums are more powerful, and enum types are distinct, even if they represent the same values. As a result, it is not possible to compare two enums for equality unless they are of the same type.

For example, to create a simple enum for representing gender:

```sh Defining a Gender enumeration Welcome to Swift! Type :help for assistance. 1> enum Gender {

  1. case Male
  2. case Female
  3. } ```

There’s no comparison or ordering between different elements; they are just a property which can be one or the other. The enumeration type can be used as an argument type, return type, or variable type using a dot syntax as with structures:

```sh Using a Gender enumeration 5> let alblue:Gender = Gender.Male alblue: Gender = Male 6> alblue == Gender.Male $R0: Bool = true 7> alblue == Gender.Female $R1: Bool = false

Since Swift knows that the type of the variable is `Gender`, it is not
necessary to supply the type of the enumeration at the point of use. This
has exactly the same effect:

```sh Using a Gender enumeration with just the enum type
  8> let alblue:Gender = .Male
alblue: Gender = Male
  9> alblue == .Male
$R2: Bool = true
 10> alblue == .Female
$R3: Bool = false

This is commonly seen when passing in flags or other properties in a Swift function. In particular, most of the existing constants in the Objective-C codebase (such as UIDeviceOrientation) are exposed as Swift enumerations, meaning that just the value can be used (such as .UIDeviceOrientationPortrait).

Enumerations can also store values along with their types as well. For example, in nuclear physics, atoms are made up of Quarks, which can have an associated colour property of Red, Blue or Green. These can be represented as enumerations as follows:

```sh Representing Quarks in Swift 11> enum QuarkColor {

  1. case Red, Blue, Green
  2. } 14> enum Quark {
  3. case Up(colour:QuarkColor)
  4. case Down(colour:QuarkColor)
  5. case Charm(colour:QuarkColor)
  6. case Strange(colour:QuarkColor)
  7. case Top(colour:QuarkColor)
  8. case Bottom(colour:QuarkColor)
  9. } 22> let proton = [
  10. Quark.Up(colour:.Red),
  11. Quark.Up(colour:.Green),
  12. Quark.Down(colour:.Blue)
  13. ] proton: [Quark] = 3 values { [0] = Up = { colour = Red } [1] = Up = { colour = Green } [2] = Down = { colour = Blue } } 27> let neutron = [
  14. Quark.Up(colour:.Red),
  15. Quark.Down(colour:.Green),
  16. Quark.Down(colour:.Blue)
  17. ] neutron: [Quark] = 3 values { [0] = Up = { colour = Red } [1] = Down = { colour = Green } [2] = Down = { colour = Blue } } ```

The values in an enum can also be used in patterns in a switch statement. For example, to represent a suit of cards it’s possible to represent both suits and face values:

```sh Representing cards as enums 32> enum Suit {

  1. case Clubs, Diamonds, Hearts, Spades
  2. } 35> enum Rank:Int {
  3. case Two = 2, Three, Four, Five
  4. case Six, Seven, Eight, Nine, Ten
  5. case Jack, Queen, King, Ace
  6. } 40> enum Card {
  7. case Face(Rank, Suit)
  8. } ```

In the same way that values like Card.Face(.Ace,.Spades) can be used to create a card, it can be used to match a card as well:

```sh Switching on enums 43> let card:Card = .Face(.Ace,.Spades) card: Card = Face { Face = { 0 = Ace, 1 = Spades } } 44> switch card {

  1. case .Face(.Ace,_): println(“Smoke me a kipper”)
  2. case .Face(_,.Hearts): println(“♥”)
  3. default: println(“Try again”)
  4. } Smoke me a kipper ```

The use of enums as pattern matchable objects is very useful for fungible data structures, like structs. However enums aren’t expected to be fully fledged objects; rather, they are useful at representing orthogonal states for a particular system; whether it be a quark or a card.

In next week’s episode, we’ll look at how to do more functional programming in Swift. To subscribe to this series, add the Swift tag feed to your reader.