Hi,
I am using this code to spell check the text I type in my UITextView.
My code below is similar to the one used in the Spell Check and Word Completion section of apple documentation
In my UITextChecker, I type in "Thiis is a speell check"
When I click the button, it calls the didPressButton function and it starts the process of spell checking the string in the UITextView. when it reaches "speell" it throws out a wrong range and it crashes the application. I logged the range and when I had a breakpoint before the if (currentRange ... ) code. looking at the log output, I saw that it returns a wrong range after it does the spell check for "speell".
The app crashes on the line "self.textView.text = [self.textView.text stringByReplacingCharactersInRange:currentRange withString:[guesses objectAtIndex:0]];"
I am unable to figure out what is going wrong.
It would be really great if someone could help me figure this out.
I am using this code to spell check the text I type in my UITextView.
My code below is similar to the one used in the Spell Check and Word Completion section of apple documentation
Code:
- (IBAction)didPressButton:(id)sender
{
NSInteger currentOffset = 0;
NSRange currentRange = NSMakeRange(0, 0);
NSString *theText = self.textView.text;
NSRange stringRange = NSMakeRange(0, theText.length-1);
NSArray *guesses;
BOOL done = NO;
NSString *theLanguage = [[UITextChecker availableLanguages] objectAtIndex:0];
if (!theLanguage)
theLanguage = @"en_US";
while (!done)
{
currentRange = [self.checker rangeOfMisspelledWordInString:theText range:stringRange
startingAt:currentOffset wrap:NO language:theLanguage];
NSLog(@"currentRange : %d", currentRange.location);
if (currentRange.location == NSNotFound)
{
done = YES;
continue;
}
guesses = [self.checker guessesForWordRange:currentRange inString:theText
language:theLanguage];
NSLog(@"Word misspelled is %@", [theText substringWithRange:currentRange]);
NSLog(@"Possible replacements are %@", guesses);
self.textView.text = [self.textView.text stringByReplacingCharactersInRange:currentRange
withString:[guesses objectAtIndex:0]];
currentOffset = currentOffset + (currentRange.length-1);
NSLog(@"currentOffset : %d", currentOffset);
}
}
In my UITextChecker, I type in "Thiis is a speell check"
When I click the button, it calls the didPressButton function and it starts the process of spell checking the string in the UITextView. when it reaches "speell" it throws out a wrong range and it crashes the application. I logged the range and when I had a breakpoint before the if (currentRange ... ) code. looking at the log output, I saw that it returns a wrong range after it does the spell check for "speell".
The app crashes on the line "self.textView.text = [self.textView.text stringByReplacingCharactersInRange:currentRange withString:[guesses objectAtIndex:0]];"
I am unable to figure out what is going wrong.
It would be really great if someone could help me figure this out.
Last edited: