How to iterate over an enumeration type?

I was trying to make a generic function to iterate over all cases of an enumeration type, given as the function parameter.


The function will take an enumeration type, and make an array of length 81, with all 3 cases present exactly 27 times.


This is how I am currently doing it:


func cardAttributeFactory<T: Sequence>(SGAttribute: T.Type) -> [T] {
        var factory = [T]()
        for attribute in SGAttribute {
            factory.append(contentsOf: Array(repeating: attribute, count: 27))
        }
        return factory
}

// below is how I am planning to call the function cardAttributeFactory

enum SGColor { case red, green, blue }
enum SGSymbol { case circle, square, triangle }

let colors = cardAttributeFactory(SGColor)
let symbols = cardAttributeFactory(SGSymbol)



The error happens on line 3, indicating: Type 'T.Type' does not conform to protocol 'Sequence'.



What would be a correct way to do it? Thanks!

Accepted Reply

There currently isn't any way of getting an enum to provide its cases as an enumerable sequence. You must manually build an array of 3 elements for each of your enums, and pass the array into the factory method.


The Swift language is getting a change that will help with this. A "evolution" proposal has just been accepted (github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md) that provides a static "allCases" property on enums that meet some basic requirements.


The very last section of the proposal ("Metatype conformance to Collection") talks about the exact feature you wanted — enumerating over the metatype to get its cases — but it's still being discussed, so any implementation is still pretty far away.

Replies

There currently isn't any way of getting an enum to provide its cases as an enumerable sequence. You must manually build an array of 3 elements for each of your enums, and pass the array into the factory method.


The Swift language is getting a change that will help with this. A "evolution" proposal has just been accepted (github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md) that provides a static "allCases" property on enums that meet some basic requirements.


The very last section of the proposal ("Metatype conformance to Collection") talks about the exact feature you wanted — enumerating over the metatype to get its cases — but it's still being discussed, so any implementation is still pretty far away.

Found this if that may help


h ttps://stackoverflow.com/questions/41352594/iterating-through-an-enum-in-swift-3-0


But sorry, I could not make it work here.


Edited 3/10

You should be interested by this accepted evolution of Swift : Derived Collection of Enum Cases, Proposal: SE-0194, accepted

h ttps://github.com/apple/swift-evolution/blob/master/proposals/0194-derived-collection-of-enum-cases.md

Thank you for pointing out the proposal! Can't wait for it to be implemented in the next version of Swift (hopefully Swift 5, coming this very year?). Sorry for the late reply. I don't know why I did not receive email notifications on my question – is it supported in the developer forum? Because I know it does in the Apple Support forum.