ios - wrong boolean result within if statement -


this code should print false console print true

nsmutablearray *data = [[nsmutablearray alloc]init];  uilabel *label = [[uilabel alloc] init]; label.tag = 7;  if(label.tag <= (data.count - 1)) {     nslog(@"true"); } else {     nslog(@"false"); } 

anyone can explain this?

data.count 0 unsigned integer (nsuinteger).

(data.count - 1) 0 - 1 in case won't equal -1 because integer unsigned. maximum integer (4294967295). call integer underflow.

you can fix

label.tag + 1 <= data.count 

with unsigned integer, have take care subtraction. way fix using cast signed integer:

label.tag <= ((nsinteger) data.count) - 1 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -