Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Santa Claus doesn’t exist (according to AI) • Graham Cluley

    December 28, 2025

    ios – Background Assets Framework server connection problem

    December 27, 2025

    FaZe Clan’s future is uncertain after influencers depart

    December 27, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»Swift enum all values – The.Swift.Dev.
    iOS Development

    Swift enum all values – The.Swift.Dev.

    big tee tech hubBy big tee tech hubJuly 23, 2025003 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    Swift enum all values – The.Swift.Dev.
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    10/11/17 2:20 PM
    · 1 min read


    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
        static var allValues: [Self] { get }
    }
    
    public extension EnumCollection {
    
        public static func cases() -> AnySequence {
            return AnySequence { () -> AnyIterator 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. 👋


    Swift enum all values – The.Swift.Dev.

    Share this article

    Thank you. 🙏

    Related posts

    featured

    Swift enum all values – The.Swift.Dev.

    6/26/25 11:00 AM
    · 8 min read


    Learn how to implement user-friendly, type-safe error handling in Swift 6 with structured diagnostics and a hierarchical error model.

    Swift enum all values – The.Swift.Dev.

    9/10/21 2:20 PM
    · 6 min read


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

    Swift enum all values – The.Swift.Dev.

    3/18/22 3:20 PM
    · 4 min read


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

    Swift enum all values – The.Swift.Dev.

    2/5/19 3:20 PM
    · 9 min read


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



    Source link

    enum Swift The.Swift.Dev values
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – Background Assets Framework server connection problem

    December 27, 2025

    ios – SwiftUI-Button: Complete VStack clickable

    December 26, 2025

    ios – ld: framework ‘Pods_MyProjectName’ not found

    December 25, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Santa Claus doesn’t exist (according to AI) • Graham Cluley

    December 28, 2025

    ios – Background Assets Framework server connection problem

    December 27, 2025

    FaZe Clan’s future is uncertain after influencers depart

    December 27, 2025

    Airbus prepares tender for European sovereign cloud

    December 27, 2025
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Santa Claus doesn’t exist (according to AI) • Graham Cluley

    December 28, 2025

    ios – Background Assets Framework server connection problem

    December 27, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.