Skip to content

Instantly share code, notes, and snippets.

@Otbivnoe
Last active February 8, 2018 03:42
Show Gist options
  • Save Otbivnoe/aa8614a641b30e1c8ef0f5b41ecad062 to your computer and use it in GitHub Desktop.
Save Otbivnoe/aa8614a641b30e1c8ef0f5b41ecad062 to your computer and use it in GitHub Desktop.
Get the all values for enum
extension RawRepresentable {
static var allValues: [Self] {
var index = 0
let iterator: AnyIterator<Self> = AnyIterator {
let current = withUnsafePointer(to: &index) { unsafePointer in
unsafePointer.withMemoryRebound(to: Self.self, capacity: 1) { unsafePointer in
unsafePointer.pointee
}
}
index += 1
return current
}
return Array(iterator)
}
}
enum Test: String {
case one
case two
}
print(Test.allValues)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment