Swift enum

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 enum
  • spring/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

// define enum 
enum Season {
  
  // define enum values
  case spring, summer, autumn, winter
}

// create enum variable
var currentSeason: Season

// assign value to enum variable
currentSeason = Season.summer

print("Current Season:", currentSeason)

Output

Current Season: summer

In the above example,

  • Season - an enum with 4 values: spring, summer, autumn, winter
  • currentSeason - enum type variable
  • currentSeason = Season.summer - assigns enum value to currentSeason

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,

enum PizzaSize {
  case small, medium, large
}

var size = PizzaSize.medium

switch(size) {
  case .small:
    print("I ordered a small size pizza.")

  case .medium:
    print("I ordered a medium size pizza.")

   case .large:
     print("I ordered a large size pizza.");
}

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

// conform Languages to caseIterable 
enum Season: CaseIterable {
  case spring, summer, autumn, winter 
}

// for loop to iterate over all cases
for currentSeason in Season.allCases {
  print(currentSeason)
}

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 10 and 12 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

enum Size : Int {
  case small = 10
  case medium = 12
  case large = 14
}

// access raw value of python case
var result = Size.small.rawValue
print(result)

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

enum Laptop {

  // associate string value
  case name(String)

  // associate integer value  
  case price (Int)
}

// pass string value to name
var brand = Laptop.name("Razer")
print(brand)

// pass integer value to price
var offer = Laptop.price(1599)
print(offer)

Output

name("Razer")
price(1599)

In the above example, the associated value

  • Razor is assigned to the name case.
  • 1599 is assigned to the price case.

To learn more about associated values, visit Swift enum Associated Value.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges