· 1 min read

Swift enum all values


In this quick tutorial I'll show you how to get all the possible values for a Swift enum type with a generic solution written in Swift.

From Swift 4.2 you can simply conform to the CaseIterable protocol, and you’ll get the allCases static property for free. If you are reading this blog post in 2023, you should definitely upgrade your Swift language version to the latest. 🎉

enum ABC: String, CaseIterable {
    case a, b, c
}


print(ABC.allCases.map { $0.rawValue })

If you are targeting below Swift 4.2, feel free to use the following method.

The EnumCollection protocol approach

First we need to define a new EnumCollection protocol, and then we’ll make a protocol extension on it, so you don’t have to write too much code at all.

public protocol EnumCollection: Hashable {
    static func cases() -> AnySequence<Self>
    static var allValues: [Self] { get }
}

public extension EnumCollection {

    public static func cases() -> AnySequence<Self> {
        return AnySequence { () -> AnyIterator<Self> in
            var raw = 0
            return AnyIterator {
                let current: Self = withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: self, capacity: 1) { $0.pointee } }
                guard current.hashValue == raw else {
                    return nil
                }
                raw += 1
                return current
            }
        }
    }

    public static var allValues: [Self] {
        return Array(self.cases())
    }
}

From now on you only have to conform your enum types to the EnumCollection protocol and you can enjoy the brand new cases method and allValues property which will contain all the possible values for that given enumeration.

enum Weekdays: String, EnumCollection {
    case sunday, monday, tuesday, wednesday, thursday, friday, saturday
}

for weekday in Weekdays.cases() {
    print(weekday.rawValue)
}

print(Weekdays.allValues.map { $0.rawValue.capitalized })

Note that the base type of the enumeration needs to be Hashable, but that’s not a big deal. However this solution feels like past tense, just like Swift 4, please consider upgrading your project to the latest version of Swift. 👋

Related posts

· 6 min read

All about the Bool type in Swift


Learn everything about logical types and the Boolean algebra using the Swift programming language and some basic math.

· 4 min read

Async HTTP API clients in Swift


Learn how to communicate with API endpoints using the brand new SwiftHttp library, including async / await support.

· 9 min read

Beginners guide to functional Swift


The one and only tutorial that you'll ever need to learn higher order functions like: map, flatMap, compactMap, reduce, filter and more.

Learn To Code Using Swift cover image

Get the Learn To Code Using Swift book

Learn To Code Using Swift is a programming book for absolute beginners, born from a fresh approach. It promises beginner-friendly explanations even for complex topics and provides up-to-date Swift knowledge. The book builds progressively from the basics to advanced concepts without expecting any prior programming experience — it clearly explains every idea, definition, and code snippet. It also teaches algorithmic thinking and reasoning skills necessary to thrive even in the age of LLMs. Theory and practice go hand in hand, with more than 350 exercises included to build confidence and solid foundations. Most of the modern Swift language features are covered, making this book a great base for anyone regardless of specific platform or interest.

Available on Gumroad