How to use UITextChecker to find misspelled word

584 views Asked by At

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.

2

There are 2 answers

1
Dharmesh Kheni On

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
1
Dilan On

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

enter image description here