Skip to content

Instantly share code, notes, and snippets.

@riteshhgupta
Last active March 26, 2017 09:21
Show Gist options
  • Save riteshhgupta/672a58b5187fbbd4681bb452becf5384 to your computer and use it in GitHub Desktop.
Save riteshhgupta/672a58b5187fbbd4681bb452becf5384 to your computer and use it in GitHub Desktop.
extension RawRepresentable where RawValue == Int {
static var itemsCount: Int {
var index = 0
while Self(rawValue: index) != nil { index += 1 }
return index
}
static var items: [Self] {
var items: [Self] = []
var index = 0
while let item = Self(rawValue: index) {
items.append(item)
index += 1
}
return items
}
}
@evermeer
Copy link

You could also do this for everything that's Hashable.
See https://github.com/evermeer/Stuff/blob/master/Source/Enum/Enum.swift#L54
And for an explanation: https://github.com/evermeer/Stuff#enum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment