What is Swift –Enumeration?
An Enumeration is a user defined data type which consists of set of related values. To represent Enumeration the keyword is enum which is used to define enumerated data type.
Enumeration Functionality
Enumeration in swift 3 is also similar to the structure of C and Objective C.
- It is declared in a class and its values are accessed using the instance of that class.
- Initial member value is defined using enum intializers.
- enum can conform to protocols to provide standard functionality.
Enumeration Syntax
Enumerations are introduced with the enum keyword and place their entire definition within a pair of braces as below −
enum enum_name {
// enumeration values are declared here
}
For example, you can define an enumeration for days of week as follows −
enum Days_of_Week {
case Sun
case Mon
case Tue
case Wed
case Thu
case Fri
case Sat
}
Example
enum names {
case Swift
case Closures
}
var lang = names.Closures // Setting the value to lan var using the enum names.
lang = .Closures // onces type is set we can use the .enumvalue to assign the var
switch lang {
case .Swift:
print(“Welcome to Swift 3”)
case .Closures: print(“enum & Closures”)
default:
print(“Introduction to swift 3”)
}
Running the above program using playground, we get the result as below −
enum & Closures
Swift enumeration does not assign its members default value similar to C and Objective C. It’s members are explicitly defined by their enumeration names. Enumeration name should begin with a capital letter (Ex: enum Days_of_Week).
var weekDay = Days_of_Week.Sun
Here the Enumeration name ‘Days_of_Week’ is assigned to a variable weekday.Sun. It tells the compiler that the datatype belongs to Sun will be assigned to subsequent enum members of that particular class.
Enumeration with Switch Statement
Switch statement in swift also follows the multi way selection. At a time only one variable is accessed at a particular time based on the specified condition for switch. Default case in switch statement is provided to trap unspecified cases.
enum Climate {
case India
case Antarctica
case korea
case Australia
}
var season = Climate. Antarcticaseason
season = . Antarcticaswitch {
case .India:
print(“Climate is very Hot”)
case .America:
print(“Climate is very Cold”)
case .Korea:
print(“Climate is Moderate”)
case .Australia:
print(“Climate is hottest in summer”)
default:
print(“Climate is unpredictable”)}
When we run to see the result for this program using playground, we get output as −
Climate is very Cold
The program first defines Climate as the enumeration name. Then its members like ‘India’, Antarctica, Korea and ‘Australia’ are declared belonging to class ‘Climate’. Now the member Antarctica is assigned to a Season Variable. Further, Switch case will see the values corresponding to . Antarctica and it will branch to that particular statement. The output will be displayed as “Climate is very Cold”. Similar all the members can be accessed through switch statements. If the condition is not present it will execute the default option that will print default value ‘Climate is unpredictable’.
Enumeration can be further classified into two type associated values and raw values.
Difference between Associated Values and Raw Values
Associated Values | Raw Values |
Different Datatypes Present | Same Datatypes Present |
Ex: enum {11,0.9,”Start”} | Ex: enum {10,45,70} |
Values are created based on constant or variable | Prepopulated Values |
Varies when declared each time | Value for member is same |
Enum with Associated Values
enum Student {
case Name(String)
case Mark(Int,Int,Int)
}
var student_Details = Student.Name(“Swift 3”)
var student_Marks = Student.Mark(198,197,195)
switch student_Marks {
case .Name(let student:_Name):
print(“Student name is as follows :: \(student_Name).”)
case .Mark(let Mark1, let Mark2, let Mark3):
print(“Student Marks present in 3 subjects are: \(Mark1),\(Mark2),\(Mark3).”)
default: print(“Nothing”)}
After running this code using playground, we get result :: −
Swift 3
198
197
195
var student_Details = Student.Name(“Swift 3”)
var student_Marks = Student.Mark(198,197,195)
Above switch case prints student name if that case block is executed else it will print the marks secured by the student. If both the conditions doesn’t match, the default block gets executed.
Enum with Raw Values
Raw values can be represented by any value characters, strings, or any of the integer or floating-point number data types. Each raw value must be unique within its enumeration declaration. Suppose we use integers for raw values, they are auto-increment if no value is specified for some of the enumeration members.
enum Month: Int {
case January = 1, February, March, April, May, June, July, August, September, October, November, December
}
let yearMonth = Month.June.rawValue
println(“Value of this Month is: \(yearMonth).”)
Running the above code using playground, we get the following result as follows−
Value of this Month is: 6