2

How to extract an NSUInteger index from

NSRange range = [string rangeOfString: substring]

so I can write this index in [array objectAtIndex:index]?

wagashi
  • 894
  • 3
  • 15
  • 39

2 Answers2

3
NSRange range = [string rangeOfString:substring];
NSUInteger index = range.location;
//...
omz
  • 53,243
  • 5
  • 129
  • 141
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
    }
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209