检查单词是否真实

时间:2015-07-07 21:07:30

标签: swift

我想确保一个单词是真实的,这是我的代码:

var checker: UITextChecker = UITextChecker()
var range: NSRange = NSRange(location: 0,length: (count(completeWord)))
var misspelledRange: NSRange = checker.rangeOfMisspelledWordInString(completeWord, range: range, startingAt: 0, wrap: false, language: "en_US")
var isRealWord: Bool = misspelledRange.location == NSNotFound
if isRealWord {
    println("Correct")
} else {
    println("Not Correct")
}

但即使我给它一封信,也说得对。我该怎么办?基本上,我想删除更正中的字母。

1 个答案:

答案 0 :(得分:0)

UITextChecker会在句子中为您提供拼写错误的单词。它不会在一个单词中显示错误的字母,而是在整个文本输入中显示错误的单词。

例如:

var checker: UITextChecker = UITextChecker()
let string:NSString = "Airplane is gren"
var range: NSRange = NSRange(location: 0,length: string.length)
var misspelledRange: NSRange = checker.rangeOfMisspelledWordInString(string as String, range: range, startingAt: 0, wrap: false, language: "en_US")

misspelledRange.toRange()

给你的结果为12 ..< 16,即整个单词。

您可以使用guessesForWordRange来获得正确的替代品:

let guesses = checker.guessesForWordRange(misspelledRange, inString: string as String, language: "en_US") as? [String]

(返回["green", "greg", "grep", "grew", "grey", "gran", "grin", "glen", "aren", "oren", "wren"]