Understanding Enums using Swift

Enums are a way to represent a “type” of values which are all related together. Lets say you had an application called MyFamily, which showed you different members in your family and their relationship to you every time you opened the application. Now, the relationship of every member in your family to you could only be one of the few values. This relationship could be represented by a “FamilyRelationshipType” using an enum as follows. FamilyRelationshipType is essentially a new data type.

There could be several other relationship types here but lets stick with these for now. So, we just created a new type called “FamilyRelationshipType” that we could use within our application. Every time we add a new member to our MyFamily app, all we have to do is assign them a relationship type. The code for holding a family member and assigning them a relationship would look something like this.

Lets say we also have to pass the above family member data over the network to save it in a remote database. The server side application or the remote database wouldn’t understand what GrandFather stands for but it would understand a known data type like an Int i.e. 1, 2, 3 or a String i.e. “GrandFather”, “GrandMother”, etc Thus we need to represent the above enums using a data type that both parties understand. The value that we use to represent the enum type is called “rawValue” in swift. By default, the above enum will be given raw values of 0, 1, 2, etc starting from the top and incrementing each value by 1. Or we can assign them whatever values we want as follows:

A most common place an enum comes in handy is when paired with a Switch conditional statement. For e.g. lets say that you send out gifts to your family members every once in a while and you have a function recommending you a relevant gift for each relationship type. A function to recommend gifts would be as follows:

Swift also lets you associate a value with each of the enums, called associated values. The associated values can only be accessed when using the enum through a switch statement. They are quite different from the rawValue in the sense that we can’t read them from an enum using the dot notation. Going back to our giftFor function, lets say we wanted to decide on the gifts based on the age of each of our family member. The enum holding associated values would look as follows

And the the giftFor function would be as follows. We can use let or var in the giftFor function to read the ages of family members and decide on the gifts. I have included the FamilyMember struct for clarity.

Finally, one of the coolest features of enums in Swift is to be able to have methods, including initializers, inside an enum. The giftFor method currently inside the FamilyMember struct could be moved inside the FamilyRelationType enum because it is based on the relationship type and the associated value of the relationship type enum. Refactoring the code above, we end up with the following. (i have also renamed the method giftFor to gift). The final code for the above is:

In my next post, I will walk through some additional example use cases for enums and specifically how the enums in Objective-C could be refactored with enums in Swift.