问题
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 ?
回答1:
if there is a higher probability that x==i
than y==i
then it's better to write it as x==i || y==i
as opposed to y==i || x==i
because if the first statement evaluates to true, the second one is not evaluated (it's shortcircuited)
回答2:
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.
回答3:
If you want to check a lot of objects you could use NSArray
and indexOf
method:
NSArray *matchingNumbers = @[@1,@2,@3,@4,@5,@6,@7,@8];
NSNumber *checkedNumber = @3;
if ( [matchingNumbers indexOfObject:checkedNumber] != NSNotFound)
{
//...
}
回答4:
Computers work with digital signals. All natural operations for computers are binary.
If you want to compare three things, you'll have to use two binary comparisons to make it nice for the computer.
Also you need to realize that shorter code is not necessarily faster. Often, it is just a shortcut notion for the author.
So why do you think you need to shorten this expression further?
回答5:
As for your question where you compared 50 values.
Make an NSMutableArray composed of those 50 variables... then use this code.
Use this code:
if ([myArray containsObject:i]) {
Works like a charm!
If you want to check each value separately you could 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...
}
}
The parts below are for others who needed different answers to this question:
There could be ways to shorten your whole conditional statement if we had the rest of the code... as for shortening just the "OR" part I'm not sure how...
But something like:
if ((i==x)||(i==y)) {
string=@"hello";
}
could become:
string = ((i==x)||(i==y)) ? @"hello";
or... something like:
if ((i==x)||(i==y)) {
string=@"hello";
} else {
string=@"goodbye";
}
could become:
string = ((i==x)||(i==y)) ? @"hello" : @"goodbye";
Since I often use conditional statements with numeric values I often use a shortcut where if a 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 is equal to x or y it will return true (which is "1") which will return 1*(z+w) or (z+w) but if the condition is false it returns "0" which returns 0*(z+w) or 0.
Final notes: *There could be mathematical ways to represent a function hat 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 being the absolute value)
回答6:
If you are using TONS of variables you could 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]) {
来源:https://stackoverflow.com/questions/14919800/anyway-to-shorten-if-i-x-i-y