Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

In this quick reference guide to Swift Enumerations or more colloquially referred to as Swift Enums I will cover exactly what is enumeration, How you can create a Swift Enum, how to add extra information using associated values and give you an understanding of raw values.

tutorial swift enum

Swift 2 Enum

As of the new update, the Swift Enum can now take multiple types:

enum Either {
    case First(T1)
    case Second(T2)

let a:Either = .First("String")

There are also some changes to Swift Enum Raw types

What is a Swift Enumeration?

Well, to quote the official documentation:

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Enumerations in Swift are first-class types in their own right. They adopt many features traditionally supported only by classes, such as computed properties to provide additional information about the enumeration’s current value, and instance methods to provide functionality related to the values the enumeration represents. Enumerations can also define initializers to provide an initial member value; can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.

Documentation on Swift Enums

OK, so seriously, what is a Swift Enumeration?

Well, it turns out that the documentation is actually pretty accurate. It does make them sound more complicated than they really are however (programmers like to do that), the easiest way to learn what they are is to just play with them. Let’s open a new Swift playground and start playing.

To create an enumeration or as it is referred to in swift enum we do so like this, lets make an enum that holds possible options for exits from a house. We will call it DoorUsed.

enum DoorUsed {
    case FrontDoor
    case BackDoor
    case SideDoor
    case GarageDoor

The values we list are referred to as member values of the Swift Enumeration. We use the case keyword to indicate that a new line of member value is about to be defined. The important take away here is that Swift enums when used can only have the possible values you list inside the enum. Note that you can define them all on one line by using a comma separator. So the above can also be defined like so:

enum DoorUsed {
    case FrontDoor, BackDoor, SideDoor, GarageDoor

Each of member value (sometimes just referred to as member) defines a new type and the name should start with a capital letter. When it comes to using this new enumeration there are a couple of different ways we can do it just like any variable declaration.

// Method 1 is to declare the variable and assign the value at the same time.
// type inference will give it a type of DoorUsed. 

var peterUsedDoor = DoorUsed.FrontDoor

Notice the first advantage of using an enum, when you type DoorUsed. Auto complete only gives you the four available options anything else you type will generate an error. So this gives us the ability to only use available options and not set a value that will cause problems.

Method 2 is to declare the variable and assign the type but not an initial value, you will see this one a lot so pay attention to the syntax.

var paulUsedDoor: DoorUsed

// Now let's set the value and notice the shortened way of doing that, this one is out
// there in the wild my friends so get used to it.

paulUsedDoor = .BackDoor

In method 2 we can simply set the value by specifying .BackDoor as the type has already been set (explicitly-typed), the same rules will apply regarding possible values.

Another advantage when using enumerations is assessing the value of a variable of that type since every possible value is already known (remember enumerations contain all the possible values). To demonstrate this we can use a switch statement, you should note that unlike a normal switch statement in Swift, we do not need to define the default case as all possible values are already known for the enumeration but you can if you wish (but why would you other than you did something wrong in the first place?)

switch peterUsedDoor {
    case .FrontDoor:
        println("Front door was used")
    case .BackDoor:
        println("Back door was used")
    case .SideDoor:
        println("Side door was used")
    case .GarageDoor:
        println("Garage door was used")

// Output in the playground should show :Front door was used".

You must specify a case for every possible value in the enumeration otherwise the compiler will tell you that you have a problem “switch must be exhaustive”, and you really do not want to make the compiler mad! To test this just delete one of the case statements and see what happens.

Swift Enum Associated Values

An associated value gives us the ability to attach extra information to each value of an enum. In other languages these are sometimes referred to as discriminated unions or tagged unions. An associated value in Swift can be of any type and each case can have it’s own types if needed, for example one case might have an image and another an audio type.

To demonstrate this we will create a new swift enumerations with a case than can hold an associated value of type String and another than can hold 3 value types of int in a Tuple.

enum ExampleAssociation {
    case Name(String)
    case Nums(Int, Int, Int)

Now we will create two different variables to demonstrate using this new enumeration.

var myName = ExampleAssociation.Name("Peter")
var someNums = ExampleAssociation.Nums(4, 12, 16)

Again we can check the values with a switch statement if we want to, but this time we can also access the associated values.

switch myName {
    case .Name (let name):
        println("The value of name is \(name)")
    case .Nums(let value1, let value2, let value3):
        println("The value of value1 is \(value1)")
        println("The value of value2 is \(value2)")
        println("The value of value3 is \(value3)")

Now you might be thinking hey wait why do we have to use the case .Nums in the switch for this variable? The answer is to remember that you must provide a case for every possible value of the enumeration. The same applies for the second variable.

switch someNums {
    case .Name (let name):
        println("The value of name is \(name)")
    case .Nums (let value1, let value2, let value3):
        println("The value of value1 is \(value1)")
        println("The value of value2 is \(value2)")
        println("The value of value3 is \(value3)")

// You can save some typing and assign the let after the case, so you could do

switch someNums {
    case let .Name (name):
        println("The value of name is \(name)")
    case let .Nums (value1, value2, value3):
        println("The value of value1 is \(value1)")
        println("The value of value2 is \(value2)")
        println("The value of value3 is \(value3)")

So clearly you might consider this some what of a draw back, but this example was just to show you how to access the associated values. You should also consider using the associated values in a better way than this example as they are assigned to constants but could they be undefined or null ..? There is your home work to find out and if so how do you deal with it (if any … no hints)!

Raw Values

Update Swift 2 Enum
Now, with the new update to the Swift Programming Language, Swift Enums no longer need to have Raw Values. The raw value will be of the same type for all members and the value for each member must be unique. When integers are use they auto increment is a value is not defined for a member. For example:

Now, with the new update to the swift Programming Language you no longer need to specify a raw value. The raw value will be the same type for all members of the swift enum and each one will be unique. If you were to use integers, they would auto-increment. Pretty neat right? Here is an example

enum Direction: Int {
    case Up = 1
    case Down // raw value 2
    case Left // raw value 3
    case Right // raw value 4

Also, If the element of an enum with string raw type does not have an explicit raw value, it will default to the text of the enum’s name. So this:

enum CardinalDirection: String {
    case North, East, South, West

is the same as this:

enum CardinalDirection: String {
    case North = "North"
    case East = "East"
    case South = "South"
    case West = "West"

Where as associated values let you specify the value outside of the enumeration, raw values give you the ability to declare a value inside the enum BUT you must specify the type for the enumeration using this technique.

enum AnotherEnum  : String {
    case Name = "Peter"
    case WebSite = ""

var myName = AnotherEnum.Name
var myWebsite : AnotherEnum
myWebsite = .WebSite

// Now let's access those values using the handy .Raw()
myName.toRaw() // Outputs "Peter"
myWebsite.toRaw() // Outputs ""

But what if a variable has no value? We should handle this in the appropriate way that by now I think as a Swift coder you are getting used to optionals (see my post on Optionals. Your homework is to figure out how you might deal with this situation, I have to give you something to take away and try right!

So, that is the introduction to Swift Enums, I hope by now you can see the possible uses and power of using Swift Enums over just having variables set to any kind of value without regard to constraints. Enumerations are a great way to control the out come of variables and deal with those possible options and only those.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

Web and Application Developer / Designer. I write about tools, techniques and tips for working with web and iOS technologies.