In any case, to shorten if (i == x || i == y)?

I tried to shorten the code using:

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

to

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

I know this is wrong because I was wrong in the magazine.

However, is there any method for shortening code in objective-C?

+4
source share
6 answers

if there is a higher probability that x==i than y==i , then it is better to write it as x==i || y==i x==i || y==i as opposed to y==i || x==i y==i || x==i , because if the first statement evaluates to true, the second does not evaluate (it is shorted)

+8
source

You can use the switch , but you really wonโ€™t buy you just 2-3 values.

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

It will be more savings if the thing you checked was more complex or you had a lot more opportunities.

+9
source

If you want to test many objects, you can use the NSArray and indexOf :

  NSArray *matchingNumbers = @[@1,@2,@3,@4,@5,@6,@7,@8]; NSNumber *checkedNumber = @3; if ( [matchingNumbers indexOfObject:checkedNumber] != NSNotFound) { //... } 
+5
source

Computers work with digital signals. All natural operations for computers are binary.

If you want to compare three things, you will need to use two binary comparisons to make it pleasant for a computer.

You also need to understand that shorter code is not necessarily faster . Often this is just a short concept for the author.

So, why do you think you need to further reduce this expression?

+3
source

As for your question, where did you compare 50 values.

Create an NSMutableArray consisting of these 50 variables ... then use this code.

Use this code:

 if ([myArray containsObject:i]) { 

It works like a charm!


If you want to check each value separately, you can use a while loop or a for loop:

 n = 0; while (n<49) { if (n==[myArray objectAtIndex:n]) { //OR statement returned true for one of the 50 objects... } } 





Parts below are for others who need different answers to this question:



There may be ways to shorten your conditional statement if we still have the code ... as for shortening only the "OR" part, I'm not sure how ...

But something like:

 if ((i==x)||(i==y)) { string=@ "hello"; } 

can be:

 string = ((i==x)||(i==y)) ? @"hello"; 





or ... something like:

 if ((i==x)||(i==y)) { string=@ "hello"; } else { string=@ "goodbye"; } 

can be:

 string = ((i==x)||(i==y)) ? @"hello" : @"goodbye"; 





Since I often use conditional operators with numerical values, I often use a shortcut where, if the conditional statement is true, it is equivalent to 1, whereas if it is false, it is equivalent to 0, because of this I can say things like

 myNumber = ((i==x)||(i==y))*(z+w); 

If I equal x or y, it will return true (which is equal to "1"), which will return 1 * (z + w) or (z + w), but if the condition is false, it returns "0", returns 0 * (z + w) or 0.





Final notes: * There may be mathematical ways to represent the hat function that returns the desired results you want ... for example, if x = -2 and y = 2, than instead of checking if I == x or I == y, just check if abs (i) == y (* abs is the absolute value)

+3
source

If you use tons of variables, you can do something like this :)

 if ([[[NSMutableArray alloc] initWithObjects:x,y,z,a,b,c,d,e,f,g,h,j,k,l,nil] containsObject:i]) { 
0
source

All Articles