Ortensia C.
Ortensia C.

Reputation: 4716

Logical operators and wrong result in Objective C

I got the x and y by a UITapGestureRecognizer set on the screen, after having obtained the location I find the object touched, so I put the conditions, but does not work. Maybe I put the wrong conditions in objective C? Xcode gives no errors but the function does not work.

  -(void)tappedMapOniPad:(int)x andy:(int)y{
       NSLog(@"the x is: %d", x);
         //the x is: 302
       NSLog(@"the y is: %d", y);
         //the y is: 37

       if((121<x<=181) && (8<y<=51)){ //the error is here
            self.stand = 431;
        }else if ((181<x<=257) && (8<y<=51)){
            self.stand=430;
        }else if ((257<x<=330) && (8<y<=51)){
            self.stand = 429;
        }

      NSLog(@"The stand is %d", self.stand);
        //The stand is 431

   }

How can I do?

Upvotes: 0

Views: 513

Answers (4)

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

(121<x<=181) kind of expression is invalid in Obj-c.

Use, (x>121 && x<=181)

Your full code will be like :

    if((x>121 && x<=181) && (y>8 && y<=51)){ //the error is here
        self.stand = 431;
    }
    else if ((x>181 && x<=257) && (y>8 && y<=51)){
        self.stand=430;
    }
    else if ((x> 255 && x<=330) && (y>8 && y<=51)){
        self.stand = 429;
    }

Or you can optimize it as:

if(y>8 && y<=51){
    if (x> 257 && x<=330) {
        self.stand = 429;
    }
    else if(x>181){
        self.stand=430;
    }
    else if(x>121){
        self.stand = 431;
    }
}

Upvotes: 1

P.J
P.J

Reputation: 6587

Missing &&

Try

if((121<x&&x <=181)&&(8<y&&y <=51))

Hope it helps

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

121<x<=181

let's assume x := 10 121<10<=181 -> false<=181 -> 0<=181 -> true

you have to do it step by step.

((121 < x) &&  (x <=181))

let's assume x := 10 ((121 < 10) && (10 <=181)) ->false && true ->false

Upvotes: 5

Pulkit Goyal
Pulkit Goyal

Reputation: 5654

Replace

if((121<x<=181) && (8<y<=51))

by

if((121 < x && x <= 181) && (8 < y && y <= 51))

Upvotes: 4

Related Questions