What is the CaseIterable Protocol in Swift?
Iterate over an enum’s cases
Published in
2 min readAug 9, 2020
In this quick tutorial, we will learn about the CaseIterable
protocol in Swift. This handy protocol allows us to iterate over an enum’s cases.
Let’s Start
Consider the following simple enum:
By conforming it to the CaseIterable
protocol, we can get access to the allCases
property, which allows us to iterate over each case:
Say we want to construct a String
that shows all available application statuses.
We can use .map
and .joined
functions for that:
The message is printed as expected:
Wrapping Up
Want to learn about other handy Swift protocols? Check out these stories below:
- What is the ExpressibleByIntegerLiteral Protocol in Swift?
- When to Use the Comparable Protocol in Swift
- What is the CustomStringConvertible Protocol in Swift?
- What is the Equatable Protocol in Swift?
Thanks for reading!