In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values.
We use the enum keyword to create an enum. For example,
enum Season {
case spring, summer, autumn, winter
}
Here,
Season- name of the enumspring/summer/autumn/winter- values defined inside the enum
Note: Enum values are also called enum cases. And, we use the case keyword to declare values inside the enum.
Create enum variables
Since enum is a data-type, we can create variables of enum type. For example,
var currentSeason: Season
Here, currentSeason is a Season type variable. It can only store values (spring, summer, autumn, winter) present inside the enum.
Assign values to enum variables
We use the enum name and the . notation to assign values to an enum variable. For example,
// assign summer to currentSeason variable
var currentSeason = Season.summer
Here, we have assigned the member value summer to the enum variable currentSeason.
Example: Swift Enumeration
Output
Current Season: summer
In the above example,
Season- an enum with 4 values:spring,summer,autumn,wintercurrentSeason- enum type variablecurrentSeason = Season.summer- assigns enum value tocurrentSeason
Note: Similar to variables, we can also create enum variables and assign value in a single line. For example,
var lastSeason = Season.spring
Swift enum With Switch Statement
We can also use an enum with a switch statement in Swift. For example,
Output
I ordered a medium size pizza.
In the above example, we have created an enum named PizzaSize with values: small, medium, large. Notice the statement,
var size = PizzaSize.medium
Here, we are assigning the value medium to the enum variable size.
We have used the enum variable size inside the switch statement. And, the value is compared with the value of each case statement.
Since the value matches with case .medium, the statement inside the case is executed.
Iterate Over enum Cases
In Swift, we use the CaseIterable protocol to iterate over an enum. For example,
enum Season: caseIterable {
...
}
Now we can use the allCases property to loop through each case of an enum.
for currentSeason in Season.allCases {
...
}
Example: Iterate Over enum Cases
Output
spring summer autumn winter
In the above example, we have conformed the CaseIterable protocol with the enum Season.
We then use the allCases property with the for loop to iterate over each case of the enum.
Swift enums with rawValue
In Swift, we can also assign values to each enum case. For example,
enum Size : Int {
case small = 10
case medium = 12
...
}
Here, we have assigned values 29 and 31 to enum cases small and medium respectively. These values are called raw values.
Note that we have used Int with enum name to define that enum cases can only include integer raw values.
Access enum raw values
To access the raw value of an enum, we use the rawValue property. For example,
// access raw value
Size.small.rawValue
Here, we have accessed the value of the medium case.
Example: enums With Raw Values
Output
10
In the above example, we have created the enum named Size which has a raw value of Int type.
Here, we have accessed the value of the small case using the rawValue property.
Note: Raw values can be of strings, characters, integers, or floating-point number types.
Swift enum Associated Values
In Swift, we can also attach additional information to an enum case. For example,
enum Laptop {
// associate value
case name(String)
...
}
Here, for the name case, we can attach a String type value.
Now, we can assign the associated value to the case.
Laptop.name("Razer")
Here, Razer is the associated value of the name case.
Example: enum Associated Values
Output
name("Razer")
price(1599)
In the above example, the associated value
Razoris assigned to thenamecase.- 1599 is assigned to the
pricecase.
To learn more about associated values, visit Swift enum Associated Value.