The Curious Case of Enumerations in Swift

Hey everybody. Today, I'm going to talk about Enumerations in Swift, or simply Enums.

What are Enums?

Enumerations are a way which allows us to create instances that are one of a defined list of cases. Swift is a powerful language and Swift's Enums have some advanced features that makes them unique as compared to Enumerations in other programming languages.

Basic Enums:

So, how do we define an Enumeration?

enum Days {  
    case monday
    case tuesday
    case wednesday
    case sunday
}

We define an Enumeration with the keyword enum followed by the name of the enumeration. By convention, the name of Enums and other types in Swift begin with a capital letter. If there are multiple words in the name of Enum, simply use Camel-Casing. For example, SomeOtherEnum.

The opening brace opens the body of enum, and it must contain at least one case statement. Here, we have four case values.

Creating Instances of Enumerations:

var someDay: Days = Days.sunday  
someDay = .monday

Because the compiler can make use of Type Inference in Swift, we don't need to explicitly specify the type of instance in Enums.

// Taking advantage of Type Inference of Swift's Compiler  
var someDay: Days.sunday  
someDay = .monday

Using Switch in Enums:

enum Days {  
    case monday
    case tuesday
    case wednesday
    case sunday
}
var someDay: Days = Days.sunday  
someDay = .monday

switch someDay {  
case .monday:  
    print("Shit! It's Monday!")
case .tuesday:  
    print("It's Tuesday today!")
case .wednesday:  
    print("We're getting close to weekends!")
case .sunday:  
    print("Yay! It's Sunday!")
}

Since, someDay has the value .monday, the switch statement will print Shit! It's Monday!.

While working with Switch cases, you need to make sure that it's exhaustive. It means that Switch should cover all the possible cases for the value of instance or variable on which it's called. To make sure that Switch is exhaustive, we make use of Default case. But using Default case in Switch for Enums may not be the best idea. Why? Because we need our code to be "Future Proof". Here, I have used four cases for the Enum 'Days'. What if I change my mind and decide to add three more cases to our enum. Let's see it in action.

enum Days {  
    case monday
    case tuesday
    case wednesday
    case sunday
    case thursday
    case friday
    case saturday
}
var someDay: Days = Days.sunday  
someDay = .friday

switch someDay {  
case .monday:  
    print("Shit! It's Monday!")
case .tuesday:  
    print("It's Tuesday today!")
case .wednesday:  
    print("We're getting close to weekends!")
case .sunday:  
    print("Yay! It's Sunday!")

default:  
    print("I don't know what day is today")
}

Here, we have made the following changes:

  • Added three more case: thursday, friday, saturday in our Enum Days.
  • Changed the value of someDay from .monday to .friday.
  • Added a default case to Switch statement.

Now, the above code will print: I don't know what day is today.

That's what I mean by Future-proof code. Our switch is quite exhaustive even without using default if we write case for each Enumerations value.

Make the following change in the code:

enum Days {  
    case monday
    case tuesday
    case wednesday
    case sunday
    case thursday
    case friday
    case saturday
}
var someDay: Days = Days.sunday  
someDay = .friday

switch someDay {  
case .monday:  
    print("Shit! It's Monday!")
case .tuesday:  
    print("It's Tuesday today!")
case .wednesday:  
    print("We're getting close to weekends!")
case .sunday:  
    print("Yay! It's Sunday!")
case .thursday:  
    print("Today is Thursday!")
case .friday:  
    print("Thank God, It's Friday!")
case .saturday:  
    print("Yay! It's Weekend!")
}

Using Raw Value in Enums:

We can make use of Raw Values by assigning a raw value to each case of Enum. Create a new Enumeration and assign a raw value to it.

enum ProgrammingLanguages: String {  
    case c = "C"
    case java = "Java"
    case php = "PHP"
    case js = "JavaScript"
    case swift = "Swift"
    case objC = "Objective-C"
}

let myFavoriteProgrammingLanguage = ProgrammingLanguages.swift  
print("My favorite programming language is \(myFavoriteProgrammingLanguage.rawValue)")  

The following will be outputted onto the console: My favorite programming language is Swift.

Enumerations is a pretty complex and huge topic. This post has become too long, so I'm going to stop right now. I'll cover other topics related to Enums in another blog post. But that's it for now.

Aloha!

comments powered by Disqus