gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:54 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com