Learning Swift – Enumerations

In this post, we will implement enumerations in swift.

What’s an enumeration?

According to the official documentation of Swift:

“An enumeration defines a common type for a group of related values.”

In simple words, an enumeration is a user defined data type which stores a set of values. Examples of such sets can be days of the week, days of the month, etc.

How to define enums in swift?

In swift, the enumeration is defined using the following syntax:

 

enum EnumerationName {
    // cases
}

 

Here enum is the keyword, and enumerationName is the name for the enumeration. The following example code creates an enum which stores days of the week.

 

enum DaysOfWeek{
    case monday 
    case tuesday 
    case wednesday
    case thursday
    case friday 
    case saturday 
    case sunday 
}

 

Once the enums are created, we define a variable of this enum type as the following code shows:

 

var today = DaysOfWeek.tuesday

 

When we assign DaysOfWeek.tuesday to variable today, type of today becomes an enumeration. Switch statements which we studied in this post, can be used to match individual enumeration values.

 

today = .thursday
switch today{
case .monday, .tuesday, .wednesday, .thursday, .friday:
    print("Working Days")
case .saturday, .sunday:
    print("Holidays")
}

 

After first initialization, we can assign values to the enum variables only using “.casename”, as done in the above example through “.thursday“.

The output of above program is “Working Days” as “.thursday” falls into the first switch case.

 

Learning Swift Enumeration example 1

 

Raw Values

In swift, enumerations can be initialized with raw values. In this example, we assign “Int” to the enumeration, making sure all the cases in this enumeration will have an integer raw value by default on initialization.

 

enum DaysOfWeek: Int{
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday
    case sunday
}

var today = DaysOfWeek.thursday

print("\(today.rawValue)")

 

When we fetch the raw value using “today.rawValue“, we get an output of three. Because by default, if we don’t assign values to all the cases, swift assigns “0” to the first case and the remaining cases are incremented by “1“, i.e. monday is 0, tuesday is 1, wednesday is 2, thursday is 3 and so on.

 

Learning Swift Enumeration example 2

 

Similarly, if we assign monday with 61, the output of thursday will be 64. Here, Swift assigns 61 to monday and all other values are incremented by 1.

 

Learning Swift Enumeration example 3

 

Last, we assign separate and different values to all the enumeration cases.

 

Learning Swift Enumeration example 4

 

The type of raw values in the enumerations could be float or double or string also.

 

Associated Values

The raw values are of the same type, so all cases store values of the same type. But in swift, enumeration cases can also store values of different data types. These values associated with each enumeration case are called associated values. For example the below enumeration:

 

enum EmployeeRecord{
    case name(String, String)
    case age(Int)
    case weight(Float)
}

 

Here we have an enumeration EmployeeRecord with three cases(values): name which can store two string values, age which can store one integer value and weight which can store a float value.

The next question that arises is how to initialize a variable of this enumeration type?

 

var employee = EmployeeRecord.name("John", "Doe")

 

Consider the name case which stores two strings. While initializing the variable employee with the enumeration EmployeeRecord, we fill in the values of the associated strings with “John” and “Doe“.

 

switch employee {
case .name(let first, let last):
    print("First Name: \(first)")
    print("Last Name: \(last)")
case .age(var age):
    print("Age: \(age)")
case .weight(let weight):
    print("Weight: \(weight)")
default:
    print("Invalid value")
}

 

Similarly, when we check these cases in the switch, we have to add the associated values as constants or variables to be able to use them. For the name, there are “let first” and “let second“.

 

Learning Swift Enumeration example 5

 

With this, we complete enumeration in swift. If any questions, leave them in comments below. You can find the code for this post here.