I have this code
NSString *nextSequentialFile =
[filePath stringByReplacingOccurrencesOfString:photoNumber
withString:[NSString stringWithFormat:@"%d", (index + 1)]
options:NSBackwardsSearch
range:[filePath rangeOfString:photoNumber options:NSBackwardsSearch]];
but I have this error and I don't understand beacuse it happens.
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString replaceOccurrencesOfString:withString:options:range:]: Range or index out of bounds'
It's likely that
rangeOfString:photoNumber
is out-of-range. I would recommend logging each of the values you pass to this method in order tell which is at fault.Never assume that methods like
rangeOfString:options:
return a valid range. From the docs:So obtain the range first and check for
NSNotFound
before attempting to use it.Try: