Jack Of Diamonds Jack Of Diamonds - 3 months ago 25
Swift Question

Enum class in swift

Im trying to create an enum class with a type and a function that returns all the types.

Initially I have my enum in my object class and a function that returns these as an array:

class someClass: Mappable {

enum Type: Int {
case A = 0
case B = 1
case C = 2
}
}
}

func getAllTypes() -> [Type] {
return [Type.A, Type.B, Type.C]
}
}


The reason i want to extract this out of my object class is because this type is also used in other classes and i don't want to duplicate any unnecessary code.

I can manage to subclass the enum but not the function that returns all the types in an array.

Any help will be appreciated.

Answer

Why don't you simply add a static property to your enum Type?

enum Type: Int {
    case A = 0, B, C
    static let all = [A, B, C]
}