Jaume Jaume - 16 days ago 5
iOS Question

objective-c switch case with multiple arguments

Probably a basic question but I would like to reduce some code using multiple arguments on switch case statements. Possible? Correct syntax?

switch (myInteger){
case (1): //here I would like to apply multiple arguments as case (1 || 3 || 5)
<#statements#>
break;
case (2):
<#statements#>
break;
default:
break;

Answer

You can use multiple cases right below each other.

switch (myInteger) {
    case 1:
    case 3:
    case 5:
        // statements
        break;
    case 2:
        // statements
        break;
    default:
        // statements
        break;
}