range.location应为0.我是对的吗?
NSRange range;
range = [@"beer" rangeOfString:@"beer and vodka"];
if (range.location== NSNotFound)
{
NSLog(@"Why?");
}
答案 0 :(得分:4)
因为您在“啤酒”字符串中搜索字符串“啤酒和伏特加” - 显然较短的字符串中不存在较长的字符串,因此您可以获得预期的输出。你需要的应该反过来:
NSRange range = [@"beer and vodka" rangeOfString:@"beer"];
if (range.location == NSNotFound)
{
NSLog(@"Why?");
}