I've searched for help and couldn't find it here. I have a button that checks the spelling, and then replaces the words with a guess. I have the following code inside the button:
UITextChecker *checker = [[UITextChecker alloc] init];
NSRange checkRange = NSMakeRange(0, [input length]);
NSRange misspelledRange = [checker rangeOfMisspelledWordInString:input
range:checkRange
startingAt:checkRange.location
wrap:NO
language:@"en_US"];
NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:input language:@"en_US"];
if (misspelledRange.location != NSNotFound) {
input = [input stringByReplacingCharactersInRange:misspelledRange
withString:[arrGuessed objectAtIndex:0]];
}
It does fix some words but not all of them. If it finds a word it can't guess it will crash and close the app. How do I go around this? I want it to ignore the process if it can't guess the word. the iOS Documentation and API Reference says if it can't find any, it will return an empty array, but then why does it crash?
Here is the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
You have to check that there's at least one guess. That's why it's crashing, you're trying to index into an empty array:
NSArray *arrGuessed = [checker guessesForWordRange:misspelledRange inString:input language:@"en_US"];
// Check if we have a misspelled range and any guesses
if ([arrGuessed count] > 0 && misspelledRange.location != NSNotFound) {
input = [input stringByReplacingCharactersInRange:misspelledRange withString:arrGuessed[0]];
}