Skip to content

Instantly share code, notes, and snippets.

@hyukhur
Created May 10, 2017 08:00
Show Gist options
  • Save hyukhur/b678ea85cc050b8a713533d7fb2e437f to your computer and use it in GitHub Desktop.
Save hyukhur/b678ea85cc050b8a713533d7fb2e437f to your computer and use it in GitHub Desktop.
Iterate All cases at a enum.
// Swift 3.1
func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
var i = 0
return AnyIterator {
let next = withUnsafeBytes(of: &i) { $0.load(as: T.self) }
if next.hashValue != i { return nil }
i += 1
return next
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment