-1
NSString *result = nil;
if([result rangeOfString:@"SUCCESS"].location != NSNotFound)
{
    NSLog(@"Location: %lu", [result rangeOfString:@"SUCCESS"].location);
    NSLog(@"Length: %lu", [result rangeOfString:@"SUCCESS"].length);
}
else{
    NSLog(@"NULL found:  %@", result);
}

this code shows output:

2016-05-12 18:51:44.589 TestProject[21666:207505] Location: 0

2016-05-12 18:51:44.590 TestProject[21666:207505] Length: 0

Actually It should show output:

NULL found:  (null)

Please explain the reason of that output

Muhammad Shahzad
  • 7,778
  • 20
  • 71
  • 123

2 Answers2

0

From the below link i got good explanation regarding this type of issue :

How do I check if a string contains another string in Objective-C?

Specially you can read @ucangetit's answer.

You can follow this two function ....

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
    if(otherString && [string rangeOfString:otherString].length)
        return YES;
    else
        return NO;
}

OR SIMPLY:

- (BOOL)doesString:(NSString*)string containString:(NSString*)otherString {
    return (otherString && [string rangeOfString:otherString].length);
}
Community
  • 1
  • 1
0

In my opinion, if range.length == 0 => is not found.

NSString *result = nil;
NSRange range = [result rangeOfString:@"SUCCESS"];//location=0, length=0
if(range.length > 0) //Found
{
    NSLog(@"Location: %lu", range.location);
    NSLog(@"Length: %lu", range.length);
}
Tim007
  • 2,486
  • 1
  • 9
  • 20