Last active
February 8, 2018 03:42
-
-
Save Otbivnoe/aa8614a641b30e1c8ef0f5b41ecad062 to your computer and use it in GitHub Desktop.
Get the all values for enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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