Objective c 目标-C提取rangeOfString返回的范围索引:

Objective c 目标-C提取rangeOfString返回的范围索引:,objective-c,indexing,nsrange,Objective C,Indexing,Nsrange,如何从中提取整数索引 NSRange range = [string rangeOfString: substring] 因此,我可以在[array objectAtIndex:index]?//中编写此索引,确保实际找到了子字符串: NSRange range = [string rangeOfString:substring]; NSUInteger index = range.location; //... NSRange result=[string rangeOfString:su

如何从中提取整数索引

NSRange range = [string rangeOfString: substring]
因此,我可以在
[array objectAtIndex:index]

//中编写此索引,确保实际找到了子字符串:
NSRange range = [string rangeOfString:substring];
NSUInteger index = range.location;
//...
NSRange result=[string rangeOfString:substring]; if(result.location!=NSNotFound) { //确保数组中存在索引 if(result.location<[数组计数]) { id someObj=[array objectAtIndex:result.location]; //在这里用someObj做点酷的事 } }
range.location如果在该字符串的开头找到该子字符串,则返回一个非常大的数字。为什么?你找到原因了吗?我也有同样的问题
// make sure that the substring was actually found:
NSRange result = [string rangeOfString:substring];

if (result.location != NSNotFound)
{
    // make sure that the index exists in the array
    if (result.location < [array count])
    {
        id someObj = [array objectAtIndex:result.location];
        // do something cool with someObj here
    }
}