Scott's solution is probably the one you want. But if you were looking for something more general that can be applied to arbitrary future enumerations and to resolve additional cases, you can try the following:
First, you need a method for iterating over Enum affairs. I used this implementation here: fooobar.com/questions/19331 / ...
func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> { var i = 0 return anyGenerator { let next = withUnsafePointer(&i) { UnsafePointer<T>($0).memory } return next.hashValue == i++ ? next : nil } }
Then you can create your protocol that defines the static functions you want:
protocol EnumIteratable { typealias ENUM_TYPE:Hashable, RawRepresentable = Self static func getAllValues() -> [ ENUM_TYPE ] static func getRawValues() -> [ ENUM_TYPE.RawValue ] }
I used a linked type so that allowing enumerations point their type to the protocol. getAllValues not strictly necessary, but it simplifies the logic.
Then you can define your common default implementations:
extension EnumIteratable { static func getAllValues() -> [ ENUM_TYPE ] { var retval = [ ENUM_TYPE ]() for item in iterateEnum( ENUM_TYPE ) { retval.append( item ) } return retval } static func getRawValues() -> [ ENUM_TYPE.RawValue ] { return getAllValues().map( { ( item:ENUM_TYPE ) -> ENUM_TYPE.RawValue in item.rawValue } ) } }
Finally, all you have to do is conform to this protocol every time you need to iterate over an enumeration:
enum TestType: String, EnumIteratable { case unitTest = "Unit Test" case uiTest = "UI Test" } TestType.getRawValues()
The advantage here is that I can add a new case for integrationTest , and I only need to add it in one place.
source share