Swift Enumerations
Enumerations, in simple terms, are a data type that includes only specific, predefined data. They are a collection of data with common characteristics.
Swift's enumerations are similar to those in Objective-C and C. Their functions include:
- They are declared within a class and can be accessed by instantiating the class.
- Enumerations can define initializers to provide an initial member value; they can extend their functionality based on the original implementation.
- They can conform to protocols to provide standard functionality.
Syntax
In Swift, the enum keyword is used to create an enumeration, and its entire definition is enclosed in a pair of braces:
enum enumname {
// Enumeration definition goes here
}
For example, we define an enumeration to represent days of the week:
import Cocoa
// Define the enumeration
enum DaysofaWeek {
case Sunday
case Monday
case TUESDAY
case WEDNESDAY
case THURSDAY
case FRIDAY
case Saturday
}
var weekDay = DaysofaWeek.THURSDAY
weekDay = .THURSDAY
switch weekDay {
case .Sunday:
print("Sunday")
case .Monday:
print("Monday")
case .TUESDAY:
print("Tuesday")
case .WEDNESDAY:
print("Wednesday")
case .THURSDAY:
print("Thursday")
case .FRIDAY:
print("Friday")
case .Saturday:
print("Saturday")
}
The output of the above program is:
Thursday
The values defined in the enumeration (such as Sunday, Monday, ..., and Saturday) are the member values (or members). The case keyword indicates that a new member value is being defined.
Note: Unlike C and Objective-C, Swift's enumeration members are not assigned a default integer value when they are created. In the
DaysofaWeekexample,Sunday,Monday,..., andSaturdayare not implicitly assigned values0,1,..., and6. Instead, these enumeration members have fully defined values, which are of theDaysofaWeektype.var weekDay = DaysofaWeek.THURSDAY
The type of weekDay can be inferred when it is initialized with a possible value of DaysofaWeek. Once weekDay is declared as a DaysofaWeek, you can use a shorthand syntax (.) to set it to another value of DaysofaWeek:
var weekDay = .THURSDAY
When the type of weekDay is known, you can omit the enumeration name when assigning it a new value. Using explicit typed enumeration values improves code readability.
Enumerations can be divided into associated values and raw values.
Differences Between Associated Values and Raw Values
| Associated Values | Raw Values |
|---|---|
| Different data types | Same data types |
| Example: enum {10, 0.8, "Hello"} | Example: enum {10, 35, 50} |
| Values created based on constants or variables | Pre-populated values |
| Associated values are set when you create a new constant or variable based on an enumeration member, and they can be different each time you do so. | Raw values are always the same |
Associated Values
In the following example, we define an enumeration named Student, which can be either a Name string or a Mark with associated values (Int, Int, Int).
import Cocoa
enum Student {
case Name(String)
case Mark(Int, Int, Int)
}
var studDetails = Student.Name("tutorialpro")
var studMarks = Student.Mark(98, 97, 95)
switch studMarks {
case .Name(let studName):
print("Student's name is: \(studName).")
case .Mark(let Mark1, let Mark2, let Mark3):
print("Student's marks are: \(Mark1), \(Mark2), \(Mark3).")
}
The output of the above program is:
Student's marks are: 98, 97, 95.
Raw Values
Raw values are not covered in the provided text. Raw values can be strings, characters, or any integer or floating-point values. Each raw value must be unique within its enumeration declaration.
When the raw value is an integer enumeration, you do not need to assign a value explicitly to each member; Swift will assign values automatically for you.
import Cocoa
enum Month: Int {
case January = 1, February, March, April, May, June, July, August, September, October, November, December
}
let yearMonth = Month.May.rawValue
print("The numeric month is: \(yearMonth).")
The output of the above program is:
The numeric month is: 5.