What is enumeration ?
Enumerations, or ‘enums’ for short, are a common feature in many programming languages providing a convenient way of grouping a set of related values together into a single code construct.
Think about a traffic light. The colours of a traffic light can be one of three colours – red, amber or green. They can’t be blue, they can’t be purple and they can’t be one of the millions or other colours that humans perceive.
So, inorder to build a traffic light using code we can define enum named as traffic light which will contain three values i.e Red, Amber and Green.
Three Types of Swift Enumeration
In Swift, there are three distinct categories of enumeration:
– Basic Enumerations
– Enumerations that have Raw Values
– Enumerations that have Associated Values
so lets begin one by one
Basic Enumerations
In Swift, Basic enumerations are, as you might have guessed, the simplest type of enumeration we can define. In practice, basic enumerations are similar to the enumerations that you may have experience of in C or Objective-C.
enum TrafficLightColor {
case red
case amber
case green
}
Here, TrafficLightColor is the enumeration type, Enumeration type is always written in camel case, where first letter of the word is capital and the rest letters are small. Red, amber and green are 3 different values called as enumeration cases. Enumeration cases are written as lowerCamelCase naming convention with a lowercase first letter and a capitalised letter for each subsequent word.
We can also define 3 cases on a single line
enum TrafficLightColor {
case red, amber, green
}
Assigning Enumeration Cases To Variables and Constants
We can also assign enumeration cases to a variable or constants as shown below:
let firstColor : TrafficLightColor
firstColor = TrafficLightColor.red
If the type of a variable or constant is already know and that type is an enumeration type, we can assign a new value to a variable (or initialise a constant) by omitting the enumeration type name completely and using just a period followed by the enumeration case we want to assign:
secondColor = .amber
Raw Value Enumerations
Raw values are almost like constants that are assigned to each enumeration case. They are pre-populated values that are assigned when the enumeration is first defined and remain fixed for the life of that enumeration type. On top of this, the raw values assigned to each of the enumeration cases must also all be of the same type and like the cases themselves must also be individually be unique within the enumeration (i.e. you can’t have two cases with the same raw value).
enum DayOfTheWeek : Int {
case monday = 1
case tuesday = 2
case wednesday = 3
case thursday = 4
case friday = 5
case saturday = 6
case sunday = 7
}
Most of the declaration above should be relatively familiar. The first difference though is the addition of the colon and the Int type after the enumeration name. This is where we define the type of raw value that each enumeration case will be assigned. Second difference you might come across is the "=" sign. Using equal to sign we are assigning the raw values to the enumeration cases.
In C and Objective-C the values associated with enumeration cases are generally limited to just integer values. In Swift though, this is not the case. Swift allows us to assign not only integer values but also Float, Double, Character and even String values as raw values
enum Color : String {
case red = "Red"
case amber = "Amber"
case green = "Green"
}
enum Color : Float {
case red = 1.0
case amber = "2.0
case green = 3.0
}
Implicit Raw Value Assignment
In the case of an enumeration where the raw values are either Int or String values (it doesn’t work for any of the other types such as Float or Double), Swift can automatically assign raw values to enumeration cases without us having to explicitly assign them. Take enumerations with raw values of type Int. The general rules here is that unless otherwise stated, Swift will start at zero and assign raw values to each enumeration case with each case being assigned a raw value one greater than that of the previous case.
enum ShortDayOfWeek: Int {
mon, tue, wed, thu, fri = 10, sat, sun
}
// mon = 0, tue = 1, ..., thu = 3, fri = 10, sat = 11, sun = 12
enum DayOfWeek: String {
case monday, tuesday, wednesday = "Hello", thursday, friday, saturday, sunday
}
// monday = "monday", tuesday = "tuesday", wednesday = "Hello", ... , sunday = "sunday"
Accessing the Raw Value of an Enumeration Case
When we assign raw values to enumeration cases, each enumeration case automatically gains a read-only property called rawValue which we can use to access the raw value
let day : String = DayOfWeek.monday.rawValue
// day is now "monday"
Enumeration Initialisation Using Raw Values
This way around, things are a little more complicated.
Enumeration type automatically gains an initialisation method that has a single parameter (called rawValue) of the same type as the enumerations raw value type. We can use this initialisation method to construct new enumeration values from a given raw value:
let day = DayOfWeek("monday")
Well now think about the case when we pass a string which doesnot gets matched with any of the enum cases. In that case what will happen ?. Well, it won’t surprise you then to find out that the enumerations initialisation function is a failable initialiser. Fialable initialiser returns nil when none of the case is matched. It means it returns an optional value of the enumeration type.
let day2 : DayOfWeek? = DayOfWeek(rawValue: "Tuesday")
// day is now an optional with a value of .Tuesday
let badDay : DayOfWeek? = DayOfWeek(rawValue: "Grrrr")
// badDay is now nil
This is the end of second type of swift enumeration. In the next part i will explain the third type and other important information about swift enumeration.
0 comments:
Post a Comment