How to enumerate an enum with String type?

i0S Swift Issue

An enumeration is a sub-set of contextually valid values of some super type, and of course you may wish to iterate over all valid values in your context.

Starting with Swift 4.2 (with Xcode 10), just add protocol conformance to CaseIterable to benefit from allCases:

Then this will print all possible values:

Compatibility with earlier Swift versions (3.x and 4.x)

Just mimic the Swift 4.2 implementation:

Another Essentially the proposed solution is:

Or made a utility function iterateEnum() for iterating cases for arbitrary enum types.

Here is the example usage:

outputs: