Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@riteshhgupta
Last active March 26, 2017 09:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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