4

I have a situation where I have to make a check like this:

if foo == a || foo == b || foo == c {
  //do stuff
}

is there any way to chain these operands into something smaller like IDK foo == a||b||c

4oby
  • 607
  • 10
  • 19

1 Answers1

7

Try this:

if [a, b, c].contains(foo) {
   //do stuff
}
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • I ain't no swift expert but if this creates some sort of container then tests that container contains something, then you've sacrificed performance for the sake of a few characters of typing. Or does some sort of compiler optimise this out? – Bathsheba May 20 '16 at 11:32
  • @Bathsheba basically you make an array, and search the item in the array, yes it reduces performance – 4oby May 20 '16 at 11:39