Skip to content

Instantly share code, notes, and snippets.

@mrpcalcantara
Created July 3, 2019 09:17
Show Gist options
  • Save mrpcalcantara/8dd3c54ef66a067e30dedd4c5e94302c to your computer and use it in GitHub Desktop.
Save mrpcalcantara/8dd3c54ef66a067e30dedd4c5e94302c to your computer and use it in GitHub Desktop.
Comparing enums
// Taken from https://stackoverflow.com/a/55409389
public protocol RawComparable : Comparable where Self : RawRepresentable, RawValue: Comparable { }
extension RawComparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
enum TestEnum: Int, RawComparable {
case value1
case value2
case value3
}
func checkCompare(val1: TestEnum, val2: TestEnum) -> Bool {
return val1 < val2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment