我想使用 UITextChecker 来查找错误的单词。不幸的是,我的代码没有按我的预期工作。有人可以纠正我的错误吗?
import UIKit
func isCorrect(word: String) -> Bool {
let checker = UITextChecker ()
let range = NSRange(location: 0, length: word.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(
in: word,
range: range,
startingAt: 0,
wrap: false,
language: "en"
)
return misspelledRange.location == NSNotFound
}
print(isCorrect(word: "apple"))
print(isCorrect (word: "ppale"))
谢谢你帮助我。
您的代码工作正常
func isCorrect(word:String)->Bool{
let checker = UITextChecker()
let range = NSRange(location: 0, length: word.utf16.count)
let mispelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return mispelledRange.location == NSNotFound
}
print(isCorrect(word: "apple"))
print(isCorrect(word: "ppale"))
在这里您可以找到
UITextChecker
的示例
考虑下面的例子:
func isReal(word: String) -> Bool {
let checker = UITextChecker()
let range = NSRange(location: 0, length: word.utf16.count)
let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
return misspelledRange.location == NSNotFound
}
isReal(word: "apple") //true
isReal(word: "pple") //false