April 2026
Intermediate
683 pages
25h 29m
English
In some cases, you might need to iterate through all cases of an enumeration. For instance, you might want to fill all options in a combo box of a user interface. For such cases, Swift supplies the CaseIterable protocol to enable iterations. Let’s discover the options for enumeration iteration in Swift.
It’s very easy to iterate over case values in an enumeration. All you need to do is to add the :CaseIterable suffix, as shown in Listing 7.24.
enum Priority: CaseIterable { case low case medium case high}
Listing 7.24 Iterable Enumeration
And voilà! The enumeration magically can be iterated now! But how? The answer can be seen in Listing 7.25.
// Enumerationenum Priority: ...
Read now
Unlock full access