Voloda2
Voloda2

Reputation: 12607

NSRange : incorrect behavior. Why?

range.location should be 0. Am i right ?

NSRange range;
range = [@"beer" rangeOfString:@"beer and vodka"];
if (range.location== NSNotFound)
{
    NSLog(@"Why?");
}

Upvotes: 2

Views: 238

Answers (1)

Vladimir
Vladimir

Reputation: 170839

Because you search for a string "beer and vodka" in "beer" string - obviously that longer string is not present in the shorter one, so you get expected output. What you need should probably be vice versa:

NSRange range = [@"beer and vodka" rangeOfString:@"beer"];
if (range.location == NSNotFound)
{
    NSLog(@"Why?");
}

Upvotes: 4

Related Questions