Question

Let me first write the code and then explain the problem

-(void)touchesBegan:(NSSet*)touches withEvent(UIEvent*)event{

    1: NSUInteger numTaps=[[touches anyObject] tapCount];
    2: NSString *labelText=[NSString stringWithFormat:@"Number of counts %d",
                                                                    numTaps];
    3: self.tapLabel.Text=labelText;
    4: if (numTaps==1)
    5: NSLog(@"Number of taps is 1");
}

Now, as you can see I assign tapCount value to numTaps in line 1. And in line 3 depending on the tap count my label shows number of counts 1,2,3,4....n. But then in line 4 I check if numTaps equals 1. Here comes the weird stuff. No matter how many taps a touch has the command in line 5 is always executed. So it seems like numTaps is always 1. But how come in line 2 namTaps is not always one. I'm sure I'm missing something there but can't figure out what.

Was it helpful?

Solution


From what I can see it may be, that you've forgot some ; after a lot of spaces in line 4, so it's place in invisible area of the screen(at least if you don't scroll to there). Try to put line 5 inside {} block like so

4: if (numTaps==1){
5:     NSLog(@"Number of taps is 1");
6: }

It's always good practice to put the if block inside braces, even if you have only one line of code inside. Can avoid many bugs.
Other possibility is that you've put single = in the if clause.(Happens a lot to me).

4: if (numTaps=1)

That actually assign 1 to numTaps variable so when the code goes one it is 1 inside.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top