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
Post a Comment