Last active
August 29, 2015 14:05
-
-
Save PeteC/b034ec34ce654c2e41b1 to your computer and use it in GitHub Desktop.
Comparing enums with associated values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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