Swift - 打开内置的iOS Dictionary来查找单词含义 [英] Swift - open built-in iOS Dictionary to find word meaning

查看:379
本文介绍了Swift - 打开内置的iOS Dictionary来查找单词含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my project I'd like to open iOS built-in Dictionary to find word meaning, or even better, get the meaning of the word directly in my app.

目前我发现如何使用 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, animated: true, completion: nil)
}

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

This solution allows to open the built-in dictionary if the word has a definition in the dictionaries saved in the device

这篇关于Swift - 打开内置的iOS Dictionary来查找单词含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆