How to get all enum values as an array in Swift

This article will show you how to get all enum values as an array in Swift.

In Swift, enums provide a way to define a group of related values. Sometimes, you might need to get all the values of an enum as an array for various purposes like iteration, conversion, or validation.

Here’s a step-by-step tutorial on how to get all enum values as an array in Swift.

Step 1: Define an Enum

This heading introduces the initial step in the tutorial, which is to define an enum named Color. It showcases the basic structure of the enum, including the enum cases and the conformance to two Swift protocols: CaseIterable and CustomStringConvertible.

Example

enum Color: CaseIterable, CustomStringConvertible {
    case red, green, blue, yellow

    var description: String {
        switch self {
        case .red: return "Red"
        case .green: return "Green"
        case .blue: return "Blue"
        case .yellow: return "Yellow"
        }
    }
}

In this example, the Color enum represents different colors and conforms to the CaseIterable and CustomStringConvertible protocols. The CaseIterable protocol allows us to obtain all the cases of the enum as an array, and CustomStringConvertible provides a customized string representation for each enum case.

Step 2: Access Enum Values as an Array

This heading explains how to access all enum values as an array using the allCases property. Once an enum adopts the CaseIterable protocol, you can use EnumName.allCases to obtain an array containing all the cases of the enum.

Example

let allColors = Color.allCases
print(allColors)

Here, we use the allCases property provided by the CaseIterable protocol to obtain an array (allColors) containing all the cases of the Color enum. The print statement then outputs the array to the console, showing all the available colors.

In the example, Color.allCases gives you an array containing [red, green, blue, yellow].

Step 3: Iterate Over Enum Values

After obtaining an array of enum values, you might want to iterate over them. This section demonstrates how to use a loop to iterate through the array and perform actions on each enum case.

Example

for color in Color.allCases {
    print(color)
}

This part demonstrates how to iterate over the array of enum values (Color.allCases). It uses a for-in loop to go through each color in the array and print each one to the console. This loop helps showcase the individual values contained in the enum.

Complete Example:

This heading summarizes the entire example, providing a complete code snippet that combines the enum definition, accessing enum values as an array, and iterating over the values. The sample code is given in a way that you can copy and run it to see the output, helping you understand how the concepts work together in a practical scenario.

enum Color: CaseIterable, CustomStringConvertible {
    case red, green, blue, yellow

    var description: String {
        switch self {
        case .red: return "Red"
        case .green: return "Green"
        case .blue: return "Blue"
        case .yellow: return "Yellow"
        }
    }
}

let allColors = Color.allCases
print("All Colors: \(allColors)")

for color in Color.allCases {
    print(color)
}

This section combines all the previous parts into a complete example. It defines the enum, accesses its values as an array, prints the array to the console, and then iterates over the values, printing each one separately. When you run this code, you’ll see the list of all colors and each color printed on a new line in the console.

Output:

All Colors: [red, green, blue, yellow]
red
green
blue
yellow

By following these steps, you can effectively work with enum values in Swift and leverage the CaseIterable protocol to get all enum cases as an array, making it easier to perform operations on them.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top