Swift enum (Enumerations)


In this tutorial, you are going to learn about the Swift enum (Enumerations) with some examples. Enum is the short form of Enumerations.

In Swift programming, an enum or enumeration is a user-defined data type that consists of a fixed set of related values. It enables Swift programmers to work with those values in a type-safe way within the code.

To create an enum in Swift, we use the enum keyword as you can see below:

enum City {
    case London, Chicago, Delhi, Geneva
}

In the above example, City is the name of the enum. London, Chicago, Delhi and Geneva are the values defined inside our enum.

Creating enum variable and assigning values

As enum is a data type in Swift, you can create variables for the enum type like you can see below:

var currentCity: City

In the above example, currentCity is a City type variable. We have previously assigned values in City.

In our variable of the City enum type, we can only store the values present in the enum. In our case, we can only store values between London, Chicago, Delhi, Geneva.

In the example below, we are storing a value in our enum type variable:

// assigning Geneva to the currentCity variable

var currentCity = City.Geneva

In the example above, we have assigned a member value Geneva to the enum variable currentCity.

See the full example:

enum City {
    case London, Chicago, Delhi, Geneva
}

// Creating the enum variable of City type
var currentCity: City

// assigning Geneva to the City variable
currentCity = City.Geneva

print("Current City is:", currentCity)

Output:

Current City is: Geneva

Iterate Over enum Cases in Swift

Swift provides a protocol that is the CaseIterable protocol. Using the for loop, we can easily iterate over enum cases if we set the CaseIterable protocol to the enum. We just need to set this protocol at the time of creating the enum.

Below is how to set the CaseIterable protocol for an enum:

enum City: CaseIterable {
    ....
}

And below is given how to use the for loop to iterate over the enum cases:

for currentCity in City.allCases {
  ...
}

You can see that we are using the allCases property to loop through the enum cases. Inside for loop we will write our code to get all the enum cases.

Below is an example where we are iterating over the cases values of a Swift enum:

// Creating the enum with CaseIterable protocol
enum City: CaseIterable {
    case London, Chicago, Delhi, Geneva
}

// Iterating over the cases value of enum using for loop
for currentCity in City.allCases {
  print(currentCity)
}

Output:

London
Chicago
Delhi
Geneva

In the above example, we have created an enum City with the protocol CaseIterable. After that, we use the for-loop. We are using the allCases property with the Swift for loop to iterate over the cases of the enum.

Using enum with the switch statement

Now it’s time to see how we can use the enum with the switch statement in Swift.

Below is an example where we are using an enum with a switch statement:

enum City {
    case London, Chicago, Delhi, Geneva
}

// Creating enum variable of City type
var currentCity = City.Delhi

// Using enum with switch statement
switch(currentCity) {
  case .Chicago:
    print("I am in Chicago.")

  case .Delhi:
    print("I am in Delhi")

   case .Geneva:
     print("I am in Geneva");
    
   case .London:
     print("I am in London");
}

Output:

I am in Delhi

Swift enum with rawValue

We can also assign values to each of the enum cases like you can see in the code sample below:

enum Price : Int {
  case low = 5000
  case high = 9500
  ...
}

In the above example, we have assigned 5000 to the low case and 9500 to the high case. These values we assigned in this example are called the raw values.

You can see that we have used the Int keyword with our enum name which defines that the cases in the enum only can take integer raw values.

Accessing rawValue from enum

If we want to access raw value from our enum, then we can do it easily. Below is how we can access a raw value:

// access raw value
Price.low.rawValue

Below is the example:

enum Price : Int {
  case low = 5000
  case high = 9500
}

// access raw value
var priceLow = Price.low.rawValue

print(priceLow)

Output:

5000

In our above example, we have first created an enum Price. We have created two raw values low and high that can accept only integer types of values. After that, we are accessing the raw value of low using the rawValue property in Swift.

In this example, we have created the raw values of integer types. But it can also be of strings, characters, integers, or floating-point number types.

Associated Values of Swift enum

We can also attach additional information to a Swift enum case just like below:

enum Employer {
  
  // associate value
  case fullName(String)
  ...
}

In the above code, we have attached a string type in the fullName case.

Now below is given how we are assigning the associated value to the case:

Employer.fullName("Linda Anderson")

Example of associated values in Swift Enumerations

Now see the example below:

enum Employer {
  
  // associate value of string type
  case fullName(String)
    
  //associate value of integer type
  case age(Int)
}

// Assigning string type value to fullName
var employerName = Employer.fullName("Linda Anderson")
print(employerName)

// Assigning integer type value to age
var employerAge = Employer.age(28)
print(employerAge)

Output:

fullName("Linda Anderson")
age(28)

In the example above, the string type associated value Linda Anderson is assigned to the fullName case. On the other hand, the integer type associated value 28 is assigned to the age case.