Chris Chris -5 years ago 202
Objective-C Question

iPhone objective-c: detecting a 'real' word

I need a (quick and dirty) solution to basically detect if a certain NSString is a 'real' word, that is, if it's in the dictionary. So basically, a very simplistic spell checker. Does anyone know of any way to do this? Basically I either need a file containing all words in the English dictionary (which I've searched for, but to no avail), or a way to interface with the iPhones spell checking service. Of course I would like to interface with the iPhones spell check service in a similar way to NSSpellChecker on OSX so my app will work with other languages, but at this point I'll take what I can get.

Lastly, here's some pseudo-code to better illustrate my needs:

-(BOOL)isDictionaryWord:(NSString*)word; //returns TRUE when word=@"greetings". returns FALSE when word=@"slkfjsdkl";

Answer Source

Use UITextChecker instead. The code below might not be perfect but should give you a good idea.

-(BOOL)isDictionaryWord:(NSString*)word {
    UITextChecker *checker = [[UITextChecker alloc] init];
    NSLocale *currentLocale = [NSLocale currentLocale];
    NSString *currentLanguage = [currentLocale objectForKey:NSLocaleLanguageCode];
    NSRange searchRange = NSMakeRange(0, [word length]);

    NSRange misspelledRange = [checker rangeOfMisspelledWordInString:word range: searchRange startingAt:0 wrap:NO language: currentLanguage ];
    return misspelledRange.location == NSNotFound;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download