What is the CaseIterable Protocol in Swift?

Iterate over an enum’s cases

Zafar Ivaev
Clean Software

--

Photo by Jessica Ruscello on Unsplash

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:

Thanks for reading!

--

--

Zafar Ivaev
Clean Software

Senior iOS Engineer. I write about features of Swift and iOS development practices.