0

In Swift, is it possible to turn

if (a == b) || (a == c) || (a == d) {}

into something like the following?

if a == (b || c || d) {}
Jack Guo
  • 3,959
  • 8
  • 39
  • 60

1 Answers1

4

You cannot combine the clauses that way, but this might work for you:

if [b, c, d].contains(a) { ... }
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    `contains()` has now changed to `contains(where:)`. So should it be `if [b,c,d].contains(where: {$0 == a})`? – Jack Guo Feb 14 '18 at 19:46
  • @JGuo: That is not correct. There are two different `contains()` methods. – Martin R Feb 14 '18 at 20:31
  • @JGuo, if the (inferred) type of the involved objects conforms to `Equatable`, `[b, c, d]` has a `contains(_:)` method. – rob mayoff Feb 14 '18 at 20:54
  • I see. I am comparing tuples of custom enum type, i.e. `(Character, Weapon)`. That's probably why. Thanks! – Jack Guo Feb 14 '18 at 21:03