微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift – 打开内置的iOS Dictionary来查找单词含义

在我的项目中,我想打开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
}

解决方法

我找到了Object-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,completion: nil)
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐