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
  • 33,665
  • 59
  • 184
  • 335
  • 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

14

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
  • 150,663
  • 30
  • 266
  • 294