简体   繁体   中英

How to use UITextChecker to find misspelled word

I want to use UITextChecker to find the wrong word. Unfortunately, my code does not work as I expected. Can anyone correct my mistake, please? Here is my code. https://i.stack.imgur.com/4Ib8e.png

Thanks for helping me.

Here you can find the example for UITextChecker

Consider the example below:

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

your code working properly

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"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM