Search code examples
iosswiftuitextchecker

UITextChecker in Swift 4


I get an error while using UITextChecker in Swift 4. The following code most likely works for Swift 3, I'm just not sure what's the problem in Swift 4? Here is my code:

import UIKit
var correctStr : String = String()
let str = "Helo"
let textChecker = UITextChecker()
let misspelledRange = textChecker.rangeOfMisspelledWordInString(
            str, range: NSRange(0..<str.utf16.count),
            startingAt: 0, wrap: false, language: "en_US")

        if misspelledRange.location != NSNotFound,
            let guesses = textChecker.guessesForWordRange(
                misspelledRange, inString: str, language: "en_US") as? [String]
        {
            correctStr = guesses.first

        }

and I receive this error: "use of unresolved identifier 'UITextChecker'.

Your help would be greatly appreciated.


Solution

  • The APIs rangeOfMisspelledWordInString and guessesForWordRange have been renamed, your code should read:

    import UIKit
    var correctStr : String = String()
    let str = "Helo"
    let textChecker = UITextChecker()
    
    let misspelledRange = textChecker.rangeOfMisspelledWord(in: str, range: NSRange(0..<str.utf16.count), startingAt: 0, wrap: false, language: "en_US")
    if misspelledRange.location != NSNotFound,
      let guesses = textChecker.guesses(forWordRange: misspelledRange, in: str, language: "en_US") {
      correctStr = guesses.first!
    }