Skip to content

Instantly share code, notes, and snippets.

@JohnSundell
Created August 10, 2017 15:47
Show Gist options
  • Save JohnSundell/a5aa87784caafdbd4a3c33d2fdd79dcb to your computer and use it in GitHub Desktop.
Save JohnSundell/a5aa87784caafdbd4a3c33d2fdd79dcb to your computer and use it in GitHub Desktop.
A simple way to iterate over linear Int-based enums
struct EnumSequence<T: RawRepresentable> where T.RawValue == Int {}
extension EnumSequence: Sequence {
func makeIterator() -> AnyIterator<T> {
var rawValue = 0
return AnyIterator {
let nextCase = T(rawValue: rawValue)
rawValue += 1
return nextCase
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment