iPhone目标c:检测一个“真实”字 [英] iPhone objective-c: detecting a 'real' word
问题描述
我需要一个(快速和肮脏的)解决方案来基本检测某个NSString是否是一个真实的单词,也就是说,如果它在字典中。所以基本上,一个非常简单的拼写检查。有人知道有什么办法吗?基本上,我需要一个包含英文字典中的所有单词的文件(我搜索过,但无济于事),或者与iPhone手机拼写检查服务接口的方式。当然,我想以类似于OSX上的NSSpellChecker的方式与iPhones拼写检查服务进行接口,所以我的应用程序可以与其他语言一起使用,但是在这一点上,我可以拿到我可以获得的东西。
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";
推荐答案
使用 UITextChecker 。下面的代码可能不是完美的,但应该给你一个好主意。
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;
}
这篇关于iPhone目标c:检测一个“真实”字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!