Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyway to shorten if ( i == x || i == y)?

I tried to shorten my code, from :

if(i== x || i == y || i == z )

to

if (i == ( x || y || z ))

I know this way is wrong because I got incorrect i in log.

However, is there any method to shorten the code in objective-C ?

like image 714
Hiraku Avatar asked Nov 28 '22 14:11

Hiraku


1 Answers

You could use a switch statement, but that doesn't really buy you a lot with only 2-3 values.

switch (i) {
    case x:
    case y:
    case z:
        ....some code....
        break
    default:
        ....some other code....
}

It'd be more of a savings if the thing you were checking was more complex or you had a lot more options.

like image 60
Amber Avatar answered Dec 04 '22 02:12

Amber