Swift 4中的UITextChecker

时间:2017-10-20 22:12:03

标签: ios swift uitextchecker

我在Swift 4中使用UITextChecker时遇到错误。以下代码最有可能适用于Swift 3,我只是不确定Swift 4中的问题是什么?这是我的代码:

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

        }

我收到此错误: &#34;使用未解析的标识符&#39; UITextChecker&#39;。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

已重命名API rangeOfMisspelledWordInStringguessesForWordRange,您的代码应为:

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!
}