Enumerations in Swift

Harshvardhan Arora
7 min readMay 8, 2022
Photo by Cindy Chan on Unsplash

Enumerations in Swift allow you to group related values under a common type which enables us to write type-safe code.

Common Questions
How to create Enums?
How to add properties and methods to Enums?
How to add custom initialisation for Enums?
How to conform Enums to protocols?
How to write extensions for Enums?

These are the questions I’ll be looking to answer in this article. The source code for these tutorials can be found here!

How to define Enums in Swift?

You just created an enum called Colors. Inside your enum, you create cases that are the possible values your enum can take. In this example, we create 5 difference cases for different colors. You can create as many as you need!

Notice how we name the enum Color and not Colors. This is done intentionally keeping in mind that a value of this type can only be set to a single case.

We can also write the cases in a single line —

How to initialise an Enum?

Here we create two properties, one variable and a constant. Both are of type Vehicle. Swift can automatically infer the type of the property, so now we can change our variable to something else without specifying Vehicle

Comparing Enum types

Comparison of enum types in not a simple topic, so I will break it down and explain it as we go. Till now we have only looked at the most basic way of creating enums. Enums with cases created this way are automatically comparable, as you can see from this example —

As we add more things to our Enums, comparing cases will not be so much straightforward

Matching Enum Values

Now suppose you have a property of an enum type, and you need to perform certain operation based on which case it belongs to. One option would be to compare that property to each individual case —

But it doesn’t look very neat does it? Also, there is a possibility you might miss out a case in certain scenarios. Let’s look at how we can use Switch statement to do this better —

Iterating over Enum cases

It is often useful to iterate over all the cases of an enum type. Let’s look at how we can do that —

Associated Values

As of right now, you can assign your enum case to a constant or variable and access it later in your code. But what if you wanted to store more information along with your enum cases? This is where associated values comes in. Let’s try to see it with an example —

Now you must be wondering, how does the switch statement would work with enum cases with associated values? Let’s see with an example —

Now I have tried to cover different things with each case here, let’s see it case by case —

  1. Even though the car case has an associated type Int named distance with it, we can chose to ignore it
  2. The train case has an associated type Int but has no name, but we can still assign it a name while iterating over the enum type.
    NOTE: The name distance will only be valid inside the scope of the case statement.
  3. The flight case has two associated types — a String with name airportName and a Double named travelHours. If you look inside the switch statement, we get the airportName as a variable instead of a constant and we chose to ignore the travelHours completely.

This covers most of the cases you will see/use for associated values. Now we will look at another concept for enums, which allows us to have default values for the enum case itself.

Raw Values

We saw how we can have associated values with an enum type. An alternative to that is to have default values (of same type) for each case. Let’s see it in an example —

Now let’s see how you would access these raw values —

In the above example, we have explicitly specified the raw value for each of our cases. Swift is also capable of implicitly assign raw values to our type. Let’s look at an example —

Now even though you did not specify the raw value for three, Swift automatically inferred it to be 3 since you started with 1. If you did not specify any value for the case one, Swift would have set the cases as 0, 1, 2, 3 automatically.

Initialising Enum from Raw Value

You can initialise an enum from its raw value. The initialiser is a failable initialiser. You will learn about this in the subsequent articles, but for now you can assume that it will either successfully return the enum if it can, otherwise return nil if it was unable to create an enum from the specified raw value. Let’s look at it with an example —

I use the “!” here because I know the initialiser will be successful because my raw value exists in the enum. You should avoid using this in your code as it would lead to a crash if the enum could not be initialised with that raw value.

Comparing Enums (Continued…)

We looked at comparing enums at the beginning which had no associated values or raw values attached to them. But now after looking at the complexity that enums bring along, you must be wondering how do you compare enums if they have associated or raw values?

Enums can only have either associated value or raw value but not both at the same time.

Enum’s with raw values can be compared exactly like enums without raw values. Since raw values for each case are constants and do not change, it does not impact how we compare those enums.

Now the issue is how do we compare enums with associated values? Let’s look at an example —

Now you would naturally have to urge to simple try to compare them using the “==” operator. Try it out! Let’s see what happens —

You’d also have an error saying —

This is because Swift does not know how to compare these enums. Even though it might look obvious to you that these are not the same, Swift is not aware how to reach that conclusion.

Change your enum Animal declaration to this —

The error goes away and returns you the output false.
This is known as conforming to a protocol. You will learn more about this in the upcoming articles, but for the time being you can think of it this way — Now Swift knows how to compare two instances of our Animal type.
If you do not understand this, do not worry about it as this will be covered in detail in the article on Protocols in Swift.

That’s it! That was quite a journey we covered with a lot of important concepts. Hopefully you now have a good working understanding of how enums work in Swift. Feel free to reach out to me in case you have any questions. Do follow my page to receive more articles on Swift fundamentals.

--

--