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

时间:2011-10-27 22:04:18

标签: objective-c indexing nsrange

如何从

中提取NSUInteger index
NSRange range = [string rangeOfString: substring]

所以我可以在[array objectAtIndex:index]

中写下这个索引

2 个答案:

答案 0 :(得分:3)

NSRange range = [string rangeOfString:substring];
NSUInteger index = range.location;
//...

答案 1 :(得分:3)

// 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
    }
}