我试图找到有关单词设备库的任何信息,检查字符串是否是实际单词,当我们使用键盘自动更正假设从某处获取单词时,是否有人知道如何访问此库?
答案 0 :(得分:0)
UITextChecker类可能是您在iOS上寻找的内容 https://developer.apple.com/documentation/uikit/uitextchecker
示例实施:
func isActualWord(wordToCheck: String) -> Bool {
let textChecker = UITextChecker()
let language = "en"
let range = NSRange(location: 0, length: wordToCheck.utf16.count)
let query = textChecker.rangeOfMisspelledWord(in: wordToCheck, range: range, startingAt: 0, wrap: false, language: language)
return query.location == NSNotFound
}
isActualWord(wordToCheck: "Swift") //true
isActualWord(wordToCheck: "Swifty") //false
func suggestedWords(wordToCheck: String) -> [String]? {
let textChecker = UITextChecker()
let language = "en"
let range = NSRange(location: 0, length: wordToCheck.utf16.count)
let suggestedWords = textChecker.guesses(forWordRange: range, in: wordToCheck, language: language)
return suggestedWords
}
suggestedWords(wordToCheck: "Swifty") // ["Swiftly", "Swifts", "Shifty", "Swift"]