Objective C: 18 is not greater than -1? -
i have peculiar problem here. i'm building spider grab hyperlinks webpage , put them table , i'm using nsranges
parse html document, i've run issue.
i have following line of code:
nslog(@"%lu", [datastring rangeofstring:@"contents.asp?year1" options:0 range:nsmakerange(index, datastring.length - index)].length);
this echoes 18
log, should, if put boolean statement, seeing if length greater -1:
nslog(@"%d", ([datastring rangeofstring:@"contents.asp?year1" options:0 range:nsmakerange(index, datastring.length - index)].length > -1));
this echoes 0, or false. 18 greater -1, what's problem? if switch < -1
, returns true. have type-casting unsigned long?
here's the definition of nsrange
:
typedef struct _nsrange { nsuinteger location; nsuinteger length; } nsrange;
notice both fields of type nsuinteger
, unsigned type. in fact, nsuinteger
unsigned long
.
since there no wider integer type unsigned long
, compiler promotes -1
unsigned. can't recall whether undefined behavior, on ios , mac os x has effect of treating 2's complement bit pattern of -1 unsigned integer. bit pattern, unsigned integer, maximum unsigned integer value.
thus comparison can never true.
if think -1
means "not found", mistaken. correct way check whether rangeofstring:options:range:
failed find target check whether location
of returned range nsnotfound
:
nsuinteger location = [datastring rangeofstring:@"contents.asp?year1" options:0 range:nsmakerange(index, datastring.length - index)].location bool foundit = location != nsnotfound;
Comments
Post a Comment