Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching multiple enums with a single guard case?

Tags:

enums

swift

I want to match against multiple enums and have something like this:

guard case .north = $0, case .south = $0 else { return }

Is there a way to condense this to a single statement like this?

guard case (. north, . south) = $0 else { return }

The above does not compile, but was hoping I can do something like this. Is there an alternative?

like image 568
TruMan1 Avatar asked Oct 22 '16 11:10

TruMan1


1 Answers

You can put the desired cases into a literal array and use contains to test for a match:

guard [.north, .south].contains($0) else { return }
like image 69
vacawama Avatar answered Nov 10 '22 21:11

vacawama