iPhone objective-c: detecting a 'real' word

前端 未结 2 558
Happy的楠姐
Happy的楠姐 2020-12-29 13:11

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

2条回答
  •  生来不讨喜
    2020-12-29 13:26

    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;
    }
    

提交回复
热议问题