Enumerations in Swift Programming
If you want to define a common type for a group of items or values which are related, then enumeration will help you to do so. It supports some of the features which were traditionally supported by classes only. These features include instance methods and computed properties. Initial values of members can also be defined by the use of enumerations.
They have a wide range of uses in Swift programming, and this explains why you need to understand how to use them. Most programmers view enumerations as complex, but in real sense, they are not.
Create a new Swift playground. In Swift, they are usually referred to as enum, so don’t get confused when you find this. Let us create an enum that will show the possible ways to exit from a house:
1
2
3
4
5
6
7
8
9
10
11
|
//Create an enum
enum exitDoor {
case Door1
case Door2
case Door3
case Door4
}
|
The values inside {} (curly braces) are called the member values. To define the member values in an enum, we use the case keyword. This shows that a new line for defining a member of the enum has been started. The enum above will contain only the listed above as members. Other than defining them individually, they can be defined in a single line, and you separate them with a comma as follows:
1
2
3
4
|
enum ExitDoor{
case Door1, Door2, Door3, Door4
}
|
Notice that the name of every member starts with a capital letter. To use the enumeration above, we can do as follows:
1
|
Var johnExitedThroughDoor= ExitedDoor.Door1
|
Notice that we declared a variable and assigned a value to the same variable at the same time. Again, if you were keen on autoComplete, you realized that it gave the options of only four doors. This is one of the ways used in enums to ensure accuracy.
Apart from the method mentioned above, we can also use this one:
1
2
3
|
var maryExitedThroughDoor: ExitedDoor
maryExitedThroughDoor= .Door2
|
It is very clear that from the second method, we have used specification so as to set the value. Let us demonstrate the use of switch statement to access the values of an enum:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
switch johnExitedThroughDoor {
case .Door1:
println("He used Door1")
case .Door2:
println("He used Door2")
case .Door3:
println("He used Door3")
case .Door4:
println("He used Door4")
}
|
The output on the playground will show that Door1 was used. Notice we have specified a case for every member of the enum, as failure to do so will result into an error. You can delete one of the case statements above and see what will happen. The compiler will tell that your code has a problem.
Enum Associated Values in Swift
With associated values, extra information can be added to each value of the enum. These values can be of any type. To demonstrate this, consider the enum shown below:
1
2
3
4
5
6
7
|
enum AssociationDemo {
case Name(String)
case Nums(Int, Int, Int)
}
|
We have defined two case statements, one to hold a string and the other one to hold three values of type integer. Let us define two variables as shown below:
1
2
3
|
var name = AssociationDemo.Name("John")
var exampleNums = AssociationDemo.Nums(4, 13, 17)
|
Let us to check the values using a switch statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
switch name {
case .Name (let name):
println("The name is \(name)")
case .Nums(let val1, let val2, let val3):
println("The value of integer1 is \(val1)")
println("The value of integer2 is \(val2)")
println("The value of integer3 is \(val3)")
}
|
Now we have accessed the values. We started by accessing the name, and then lastly we accessed the integers. Every value of the enum has been provided with a case.
Raw Values
With raw values, you are able to declare values inside the enum. It works just in the opposite way as associated values, since these allow you to declare values outside an enum. The type of enumeration to use the technique must be specified.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
enum Enum2 : String {
case Name = "Technotif"
case WebSite = "http://technotif.com"
}
var name = Enum2.Name
var ebsite : Enum2
website = .WebSite
|
Note the syntax we have used to access the values. Very easy. You should know how to handle enums without values, as you might try to access empty values.
1
2
3
4
5
|
// Now let’s access those values using the handy .Raw()
myName.toRaw() // Outputs "Peter"
myWebsite.toRaw() // Outputs "http://www.peterwitham.com"
|