How to enumerate an enum with String type?


In Swift, you can use the .allCases property to enumerate all cases of an enum that have CaseIterable protocol conformance. You will see an example of using the CaseIterable protocol that can help you to iterate all cases of an enum. Using different ways like a for-in loop, reduce, filter, and map functions you can iterate an enum.

What is the CaseIterable Protocol?

The CaseIterable is a protocol that is used to iterate the enum cases. It synthesized all cases automatically for an enum. Remember that, this protocol can not be applied in the case of associated values. This protocol enables you to access the cases using a computed property called allCases which returns all the cases in a collection array.

Example

For example, if you have an enum called MyEnum with String raw values −

enum MyEnum: String, CaseIterable {
   case case1
   case case2
   case case3
}

Here are a few more examples of how you can use the .allCases property to enumerate an enum with String raw values in Swift −

In this article we are going to use the following methods to enumerate an enum with string type in swift −

  • Using a for-in loop

  • Using the filter function to find a specific case

  • Using the map function to create an array of raw values

  • Using the reduce function to concatenate all raw values

Using a for-in loop

You can use the for-in loop to iterate the cases of an enumeration. You can iterate the cases using the allCases property provided by the enum.

Example

enum MyEnum: String, CaseIterable {
   case case1 = "First case"
   case case2 = "Second case"
   case case3 = "Third case"
}
for item in MyEnum.allCases {
   print(item.rawValue)
}

Output

First case
Second case
Third case

In the above example, we used the CaseIterable protocol to iterate the enum cases. Using a for-in loop, iterating the allCases in string format.

Using the map function to create an array of raw values

You can iterate the enum cases using the map() function.

MyEnum.allCases.map { $0.rawValue }

Example

Using the map() function, you can convert the type of the enum’s rawValue.

enum MyEnum: String, CaseIterable {
   case case1 = "First case"
   case case2 = "Second case"
   case case3 = "Third case"
}
let rawValues = MyEnum.allCases.map { $0.rawValue }
print(rawValues)

Output

["First case", "Second case", "Third case"]

In the above example, using the map() function to iterate the cases. Also, we are returning the rawValue for each case in the execution block of the map() function.

Using the filter function to find a specific case

You can use the filter function to find a specific case in an enum.

MyEnum.allCases.filter { $0.rawValue == "Second case" }

In Swift, you can use the filter function (a high-order function) to filter the cases whatever you want. In the above syntax, we are filtering out the cases which matched with the value “Second case”. This method returns an array as an output. Here, you are using the rawValue property as you need to match with a string value.

Example

enum MyEnum: String, CaseIterable {
   case case1 = "First case"
   case case2 = "Second case"
   case case3 = "Third case"
}
let filteredCases = MyEnum.allCases.filter { $0.rawValue == "Second case" }
print(filteredCases)

Output

[main.MyEnum.case2]

In the above example, using the filter() function filters the case matching with the value “Second case”. This function returns the values of the Enum type.

Using the reduce function to concatenate all raw values

MyEnum.allCases.reduce("") { $0 + $1.rawValue + " " }

In Swift, you can use the reduce (a high-order function) function to concatenate all the cases into a single value. In the above syntax, reduce function takes an initial value that we passed an empty string. In the completion block, we are appending the rawValue of each case with the previous value.

Example

enum MyEnum: String, CaseIterable {
   case case1 = "First case"
   case case2 = "Second case"
   case case3 = "Third case"
}
let concatenatedRawValues = MyEnum.allCases.reduce("") { $0 + $1.rawValue + " " }
print("Concatenated Raw Values:", concatenatedRawValues)

Output

Concatenated Raw Values: First case Second case Third case 

In the above example, we are concatenating the all raw values to make a single string.

Conclusion

Swift's CaseIterable interface makes it simple to enumerate every case of an enum that has no associated values. You can access all of the enum's cases as a collection of the enum's type by complying with the CaseIterable protocol and using the allCases property.

You can use the allCases property provided by the enum to iterate all the cases of an enumeration. You can filter them or perform any other action in all the cases. If you want to convert the value type, you can use the map function to convert them. In this case, you have to use the rawValue property.

You should note that you can not iterate cases if you are using the associated value for enum cases. With the associated values, the CaseIterable protocol does not apply.

Updated on: 28-Feb-2023

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements