ios - Swift - 打开内置的 iOS 字典来查找词义

标签 ios swift dictionary word

在我的项目中,我想打开 iOS 内置词典 来查找词义,或者更好的是,直接在我的应用中获取词义。

目前我发现了如何使用 UITextChecker

检查字符串是否拼写正确
func wordIsReal(word: String) -> Bool {
    let checker = UITextChecker()
    let range = NSMakeRange(0, count(word))
    let misspelledRange = checker.rangeOfMisspelledWordInString(word, range: range, startingAt: 0, wrap: false, language: "en")

    return misspelledRange.location == NSNotFound
}

最佳答案

我找到了Objective-C的解决方案:

if ([UIReferenceLibraryViewController    dictionaryHasDefinitionForTerm:@"word"]) {
    UIReferenceLibraryViewController* ref = 
    [[UIReferenceLibraryViewController alloc] initWithTerm:@"word"];
    [currentViewController presentViewController:ref animated:YES completion:nil];
}

我已经为 Swift 3 编辑了它:

let word = "home"
if UIReferenceLibraryViewController.dictionaryHasDefinitionForTerm(word) {
        let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word)
        self.presentViewController(ref, animated: true, completion: nil)
}

Swift 4也是如此:

let word = "home"
if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: word) {
    let ref: UIReferenceLibraryViewController = UIReferenceLibraryViewController(term: word)
    self.present(ref, animated: true, completion: nil)
}

如果单词在设备中保存的词典中有定义,此解决方案允许打开内置词典

关于ios - Swift - 打开内置的 iOS 字典来查找词义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32603825/

相关文章:

c++ - C++ map 中的 Char 二维数组

ios - RestKit 更新对象映射空属性

Python字典太满还是其他原因?

ios - Xcode:发送的事件在将 UIButton 类更改为自定义类时消失

swift - 在终端中执行 Swift 脚本

swift - 如何在 Swift 3 中将 "2017-01-09T11:00:00.000Z"转换为日期?

ios - 快速在 Google map 中自定义标记

python - 从 python 中的字典获取键列表/键集的最有效方法是什么?

iphone - 在 iOS 应用程序中对 Sqlite 的写访问

iphone - 带有图像的 Restkit MultiForm 帖子