Dear Erica: How do I check for multiple enums cases?

Akshay H. writes, “Is there a way to compare two enum cases with `if case`? I tried variations of if case .Case1 = foo || case .Case2 = foo { .. } and if case .Case1,.Case2 = foo { .. }”

You can easily test multiple enum cases with membership and switches. Here are examples of both.

Screen Shot 2015-06-21 at 5.18.45 PM

One Comment

  • Cool post! If this enum has an associated value that you are trying to bind, you can’t use an or condition to my knowledge. This makes sense if you think about it. If only one of the value binds and you use both values in the block of the if statement, bad things would happen. Better to use if/else. if case .Animal(let animalType) = thing { } else if case .Vegetable(let vegetableType) = thing { } else if .Mineral(let mineralType) = thing {}