
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- Enumerate over an enum in C++
- How to convert an enum type variable to a string in C++?
- How to define an enumerated type (enum) in C++?
- How do we use an enum type with a constructor in Java?\n
- How to convert a String to an enum in Java?
- What is MySQL ENUM data type? What are the advantages to use ENUM data type?
- How to use an enum with switch case in Java?\n
- Count items in a MySQL table with type ENUM involved?
- Does SQL Server have an equivalent to MySQL's ENUM data type?
- How to call another enum value in an enum's constructor using java?
- How to create Python dictionary by enumerate function?
- How do I add more members to my ENUM - type column in MySQL?
- How do we use enum keyword to define a variable type in C#?
- Java Program to Lookup enum by String value
- Golang Program to Lookup enum by String value
