How do I get a list of all possible values of a Swift enum?

This wasn't possible in Swift 1.2 - is it possible in 2.0?

Not so much. Kicked around the following but it's not all that great and here requires an underlying bounded consecutive int value. If my picture isn't showing, here's an imgur link.


I think you will still have to do it by manually creating a list of all possible values of your enum.


Perhaps it might seem strange that an enum is not enumerable ... But apart from that I don't think it is particularly surprising.


Maybe you could have a look at the new OptionSetType:

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Reference/Swift_OptionSetType_Protocol/index.html

  • I couldn't add init?(rawValue:) to the protocol for some reason. Hence the init?(index:) initializer
  • Does anyone know how to use the 'where' clause to tell if an enum has Int rawValues? I tried several approaches, but couldn't get any to work. It would be nice to remove the boilerplate init...

This works:

protocol EnumerableEnum: RawRepresentable {
static func allValues() -> [Self]
}
extension EnumerableEnum where RawValue == Int {
static func allValues() -> [Self] {
var idx = 0
return Array(anyGenerator { return Self(rawValue: idx++) })
}
}
enum TestEnum: Int, EnumerableEnum { // <-- Will not compile unless it has Int RawValues.
case A, B, C
}
print(TestEnum.allValues())



Accessing static properties from a protocol extension seems to crash the compiler at the moment, hence the static function

Yes, it seems like a compiler bug:

protocol EnumerableEnum: RawRepresentable {
static var allValues: [Self] { get }
}
extension EnumerableEnum where RawValue == Int {
static var allValues: [Self] {
get {
var idx = 0
return Array(anyGenerator { return Self(rawValue: idx++) })
}
}
}
enum TestEnum: Int, EnumerableEnum {
case A, B, C
}
//print(TestEnum.allValues) // <-- Uncomment to crash compiler, otherwise compiles w.o. complaining, file bug report?
How do I get a list of all possible values of a Swift enum?
 
 
Q