Swift 4.2: Enumerable Enum

What is CaseIterable?

Example

enum Pizza: CaseIterable {
case neapolitan
case chicago
case newYork
case sicilian
case greek
case california
}
let pizzaTypes = Pizza.allCases.map {
"\($0)"
}
print(pizzaTypes) // ["neapolitan", "chicago", "newYork", "sicilian", "greek", "california"]

Limitations

// Pizza.swift
enum Pizza: CaseIterable {
....
}
**** OR ****
enum Pizza {
....
}
extension Pizza: CaseIterable { }
// Pizza.swift
enum Pizza {
....
}

// SomeOtherFile.swift
extension Pizza: CaseIterable { }
enum Pizza {
case neapolitan(NeapolitanTypes)
case chicago
}
enum NeapolitanTypes {
case marinara
case margherita
}
extension Pizza: CaseIterable {
static var allCases: [Pizza] {
return [.neapolitan(.marinara),
.neapolitan(.margherita),
.chicago]
}
}

Conclusion

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melvin John

Melvin John

A Software Engineer with a passion for technology. Working as an iOS Developer @BBC

More from Medium

I finally used method-swizzling

I finally used method-swizzling

Xcode-How to change the main Storyboard

Preventing Invalid Characters from Being Entered in a UITextField