Skip to content

Instantly share code, notes, and snippets.

@PeteC
Last active August 29, 2015 14:05
Show Gist options
  • Save PeteC/b034ec34ce654c2e41b1 to your computer and use it in GitHub Desktop.
Save PeteC/b034ec34ce654c2e41b1 to your computer and use it in GitHub Desktop.
Comparing enums with associated values
enum MyEnum {
case StateA
case StateB
}
let enumValueA = MyEnum.StateA
let enumValueB = MyEnum.StateB
if enumValueA == enumValueB {
println("Same")
}
else {
println("Different")
}
enum AnotherEnum {
case StateA
case StateB(Int)
}
func ==(a:AnotherEnum, b:AnotherEnum) -> Bool {
switch (a, b) {
case (.StateA, .StateA):
return true
case let (.StateB(intA), .StateB(intB)):
return intA == intB
default:
return false
}
}
let anotherEnumValueA = AnotherEnum.StateA
let anotherEnumValueB = AnotherEnum.StateB(1)
if anotherEnumValueA == anotherEnumValueB {
println("Same")
}
else {
println("Different")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment