Enums in Swift - A Quick overview

Swift is a dynamically changing language as it is still so as to say Under development, however it is the exact opposite. It is a static language that relies on types, etc all for the compiler to understand and reduce errors at runtime. In our blog post today, let us explore enums and their use, they are very powerful and unlike any enums you've seen before in other languages.

How to declare enums

Declaring enums is easy, simply type

enum genres {

There you have it, you have just declared an enum, but it is not very useful as it does not have any members that you want to enumerate.

How to add enumerators to an enum

Adding members to an enum is easy and you need to use the case statement as
enum genres {
case One, Two, Three, Four, Five

and there you have it an enum called genres that have the members, One, Two, Three, Four, Five

How to use enums

Using enums is also pretty easy and straightforward

var myGenre:genres
This just declared a variable that is of type genre

Assigning an enum value

Enum values can be assigned to variables as

myGenre = genres.Four

Where the variable is assigned the value held in enum.Four. You could also avoid the use of the prefix genres and straightaway use the myGenre = .Four

Enumerating values for enum

While in most cases you do not really require a value to be enumerated as can work with the enum.member format, there may be a case where you need to assign a specific value, there are several ways to manage this

1. Using a conditional

You could use an if statement to assign values based on the enum

var result:Int = 0
if myGenre == genre.Three {
result = 1003

println("Result -> \(result)")

2. Use defined values

You could use the enum with defined values as

var genres:Int{
case One = 1001, Two, Three, Four, Five

This now enumerates the values and assigns genre.One with 1001 and genres.Two with 1002 and so on. Note that with this declaration we have also added the var type after the name of the enum. This is required if you are using the enums with a 'rawValue'
However you cannot get the value by simply using something like println("Result -> \(genres.Two)")
. To get the value stored in that enum member, you need to access it's rawValue property as
println("Result -> \(genres.Two.rawValue)")

Defined String Values

The example above was declared with values of type Int, the same can be initialised with String values too as

enum genres:String {
case One = "One", Two = "Two", Three = "Three", Four = "Four", Five = "Five"

Note all rawValues that are are not of type Int must be declared, so you cannot have partial declarations

HashValue vs RawValue

If you had the enum as we had declared earlier without the type, there is no rawValue available but instead you get a member called hashValue. All enums get a hashValue which is basically like an Index to the order in which the enums were declared. So One will have a hashValue of 0, Two of 1, Three of 2 and so on. The reason for this is that hashValues are based on the first item having the index value of 0.
These can be retrieved as
println("The value of myGenres is \(myGenre.hashValue)")

The rawValue on the other hand is a type value that you can assign to the enum members.
These can be retrieved as
println("The value of myGenres is \(myGenre.rawValue)")

That was Easy, What more?

Swift has one more cool trick, it can have functions. So you could define an enum as

Loading ....

You will see that the string "this is a string with the value of Two" is returned. You could use the functions to encapsulate and return custom results. You could have formatted strings, calculated scores and bonuses, etc. So instead of writing all this code inside of your function and every module you can now house this code in the enum declaration and have a more managed codebase.

A Cool Example Use

Let's say we want to group our developer contacts in one of the categories (read: enum) of either Ninja, Panda, Pirate or Zombie. So our first step is to create an enum as

Loading ....

Next we can create a developer that is of one of the types as

var developer:DeveloperType = .Ninja // Create this of type Ninja

Let's say we want to know the sound developers makes. We can add a function to the enum called sound that would return the sound made by that particular developerType.

Loading ....

We could simply use println("sound -> \(developer.sound())") and in this case since the developer is a Ninja, there is no sound ;) Ninjas are known for their stealth. You can add more functions that could further provide things like Weapons of choice, etc

Loading ....

Note: While you can add functions and properties, you cannot add stored properties.

The example above is for illustration purposes and the same can be better managed using other options. If you were writing a game, then you would have a factory method pattern, create DeveloperType as a protocol or an abstract class to inherit from/sub class.


This extends further also to structs, that behave almost like classes with their own functions and variables. You could even encapsulate and have private enums as part of the struct. We shall explore this in another blog post. Till then, hope you get something out of this post.


Popular Posts