Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
count and allValues on Int-based enums
protocol CaseCountable { }
extension CaseCountable where Self : RawRepresentable, Self.RawValue == Int {
static var count: Int {
var count = 0
while let _ = Self(rawValue: count) { count+=1 }
return count
static var allValues: [Self] {
return (0..<count).flatMap({ Self(rawValue: $0) })
enum Someenum: Int {
case one, two, three, four
extension Someenum: CaseCountable { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.