Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Swift enum all values – The.Swift.Dev.

learningcode_x1mckf by learningcode_x1mckf
October 11, 2022
in Swift
0
Swift enum all values – The.Swift.Dev.
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Encoding and decoding data using the Hummingbird framework

Hummingbird routing and requests – The.Swift.Dev.

Building a Scrollable Custom Tab Bar in SwiftUI

From Swift 4.2 you’ll be able to merely conform to the CaseIterable protocol, and you will get the allCases static property without cost. If you’re studying this weblog publish in 2020, you need to positively improve your Swift language model to the newest. 🎉🎉🎉

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


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

If you’re focusing on beneath Swift 4.2, be happy to make use of the following method.

The EnumCollection protocol strategy

First we have to outline a brand new EnumCollection protocol, after which we’ll make a protocol extension on it, so you do not have to put in writing an excessive amount of code in any respect.

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


public extension EnumCollection {

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

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

Any further you solely have to evolve your enum sorts to the EnumCollection protocol and you may benefit from the model new circumstances methodology and allValues property which can include all of the doable values for that given enumeration.

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


for weekday in Weekdays.circumstances() 
    print(weekday.rawValue)


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

Notice that the bottom sort of the enumeration must be Hashable, however that is not an enormous deal. Nonetheless this resolution appears like previous tense, similar to Swift 4, please think about upgrading your venture to Swift 5. Thanks for studying, bye! 👋



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Encoding and decoding data using the Hummingbird framework

by learningcode_x1mckf
March 22, 2023
0
Encoding and decoding data using the Hummingbird framework

HTTP is all about sending and receiving information over the community. Initially it was solely utilized to switch HTML paperwork, however these days we use HTTP to switch...

Read more

Hummingbird routing and requests – The.Swift.Dev.

by learningcode_x1mckf
March 17, 2023
0
Hummingbird routing and requests – The.Swift.Dev.

Routing on the server facet means the server goes to ship a response primarily based on the URL path that the consumer referred to as when firing up...

Read more

Building a Scrollable Custom Tab Bar in SwiftUI

by learningcode_x1mckf
March 10, 2023
0
Building a Scrollable Custom Tab Bar in SwiftUI

Whether or not you’re making a social media app or a productiveness device, the tab bar interface can improve the consumer expertise by making it extra intuitive and...

Read more

Beginner’s guide to server-side Swift using the Hummingbird framework

by learningcode_x1mckf
March 8, 2023
0
Beginner’s guide to server-side Swift using the Hummingbird framework

Swift on the Server in 2023 Three years in the past I began to focus on Vapor, the preferred web-framework written in Swift, which served me very...

Read more

What’s new in Swift 5.8 – Hacking with Swift

by learningcode_x1mckf
March 8, 2023
0
What’s new in Swift 5.8 – Hacking with Swift

Though many main Swift modifications are at present percolating by way of Swift Evolution, Swift 5.8 itself is extra of a clear up launch: there are additions, sure,...

Read more
Next Post
Researchers Detail Critical RCE Flaw Reported in Popular vm2 JavaScript Sandbox

Researchers Detail Critical RCE Flaw Reported in Popular vm2 JavaScript Sandbox

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

My Display Button is Not Working – JavaScript – SitePoint

February 3, 2023
Node.js creator Ryan Dahl urges Oracle to release JavaScript trademark • DEVCLASS

Node.js creator Ryan Dahl urges Oracle to release JavaScript trademark • DEVCLASS

September 5, 2022
Time limit for notify – JavaScript – SitePoint Forums

Help with modal triggered by php if statement and JS – JavaScript – SitePoint Forums

December 14, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?