7

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?

TruMan1
  • 30,176
  • 50
  • 162
  • 301
  • Possible duplicate of [How to do if pattern matching with multiple cases?](http://stackoverflow.com/questions/39333716/how-to-do-if-pattern-matching-with-multiple-cases) – Hamish Oct 22 '16 at 14:48

1 Answers1

13

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 }
vacawama
  • 141,339
  • 29
  • 256
  • 279