views:

116

answers:

2

Hi everyone,

I am trying to check a string to see if there are any misspelled words in it. I'm using apples UITextChecker class to do so like so:

#import <UIKit/UITextChecker.h>
        id textChecker = [UITextChecker new];

        NSInteger currentOffset = 0;
        NSRange currentRange = NSMakeRange(0, 0);
        NSRange stringRange = NSMakeRange(0, 2);
        NSArray *guesses;
        NSString *theLanguage = [[UITextChecker availableLanguages] objectAtIndex:0];
        if (!theLanguage)
            theLanguage = @"en_US";


    currentRange = [textChecker rangeOfMisspelledWordInString:currentText range:stringRange
                                                           startingAt:currentOffset wrap:NO language:theLanguage];

    //  NSLog("@currentRange is %i", currentRange);
        if (currentRange.location == NSNotFound) {
                NSLog(@"No wrong words");
            }
        else {
            NSLog(@"Words were found wrong");
        }
    }

It always returns "Words were found wrong", no matter what words is put in

What am I doing wrong??

Thanks!!

+4  A: 

What examples of words are you testing? I'm guessing your problem is probably the fact that stringRange is set to {0,2} so it's only checking the first two characters in the string for misspelled words. If those first two characters are not a word, then I assume that it says it's misspelled. Try setting it to {0,string.length} instead and see what happens.

Ed Marty
Hi, thanks but I tried that too. I'm only trying to check a string that is only 3 characters. (0, 1, 2) three characters. I went ahead and tried it the way you suggested...same result :/ . I'm stuck! Any help is greatly appreciated!
Bdennis317
Perhaps you should try some more NSLogs. Like, NSLog the currentText before calling the method, then NSLog the substring that it says is misspelled after finding it. And with an NSRange of 0,2 you are only checking the first 2 characters. The first value is the location, and the second is the length of the range.
Ed Marty
Oh I stand corrected on the NSRange. How can I NSLog the the NSRange. I'm not sure what to use for the pointer(?) like NSLog(@"The range is %@", currentRange); It gives me some error and crashes..What should I put in place of %@?
Bdennis317
NSRange is a struct, not an objective-c object, so you don't print it with %@. use %u with its individual components, location and length, which are unsigned integers.
zem
@zem thanks, no matter what I put in I always get 0 for the location and the length...What could I be doing wrong?
Bdennis317
+1  A: 

Final version. The code which works for me (even for a single word) -

NSRange range = NSMakeRange(0, 0);
range = [textChecker rangeOfMisspelledWordInString:[currentWord lowercaseString] 
                                             range:NSMakeRange(0, [currentWord length]) 
                                        startingAt:0 
                                              wrap:NO 
                                          language:@"en_US"];
if (range.location == NSNotFound) {
    NSLog(@"Word found");
}

Note two changes against Apple's original code:

  • language is forcefully set to @"en_US" as [[UITextChecker availableLanguages] objectAtIndex:0]; returned @"pt_PT".
  • range is (0, length) instead of (0, length - 1)
dytrivedi
So for a temporary workaround, what I did was, include my word in a sentence, and then test the sentence with their original code. Worked for me :D And also, their [[UITextChecker availableLanguages] objectAtIndex:0]; was returning "pt_PT" which I forcefully set to "en_US".
dytrivedi
Also, NSRange stringRange = NSMakeRange(0, theText.length-1); has to be changed to NSRange stringRange = NSMakeRange(0, theText.length);
dytrivedi
Right, I'll just do that for now. Thanks guy. Should we submit a bug report to apple?
Bdennis317
Yes I think we should do that!
dytrivedi
I zeroed the bug down to NSMakeRange(0, theText.length-1) and @"en_US" - just use NSMakeRange(0, theText.length) instead and it works, even for a single word. No way of detecting a proper noun though - which is a bugger in my case. E.g. I would want to know whether "Steven" is a proper noun, or not - to filter it out.
dytrivedi
haha screw it, I also figured out how to detect a proper noun, using guessesForWordRange.
dytrivedi