Created
June 17, 2021 13:00
-
-
Save denisenepraunig/b3733715d94a86ed800969575f0e1ac2 to your computer and use it in GitHub Desktop.
Switch over multiple variables
This file contains hidden or 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
import Foundation | |
enum FooEnum { | |
case foo1 | |
case foo2 | |
case foo3 | |
} | |
enum BarEnum { | |
case bar1 | |
case bar2 | |
case bar3 | |
} | |
struct BazStruct { | |
let enum1: FooEnum | |
let enum2: BarEnum | |
} | |
let baz = BazStruct(enum1: .foo1, enum2: .bar3) | |
switch (baz.enum1, baz.enum2) { | |
case (.foo1, .bar3): | |
print("foo1 and bar3 case") | |
case (.foo2, .bar3): | |
print("foo2 and bar3 case") | |
case (.foo3, .bar1): | |
print("foo3 and bar1") | |
default: | |
print("all other cases") | |
} | |
// prints: | |
// foo1 and bar3 case |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting Twitter thread by Nick Lockwood - thanks Chris Wu for pointing this out :-)
https://twitter.com/nicklockwood/status/1361621533626621952