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(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!

Powered by WPeMatico

You may also like...

Comments are closed.