本程序封装了macOS的字典服务,实现枚举所有字典、查找单词并以文本或HTML的形式获取解释。
所有实现在 macdict.swift文件中。
使用默认字典查找:
let text = DCSDictionary.lookUp(text: "word")
遍历所有字典
for (dictName, dict) in DCSDictionary.availableDictionaries {
print(dictName)
}
查找单词
let dictName = "Oxford Dictionary of English"
let word = "hello"
if let dict = DCSDictionary.getDictionary(by: dictName), let entries = dict.lookUp(text: word) {
for entry in entries {
print("-----------headword---------")
print(entry.headword)
print("-----------text-------------")
print(entry.text)
print("-----------HTML-------------")
print(entry.html)
print("---------------------------")
}
} else {
print("can not find the definition of \"\(word)\"")
}
在实现时参考了 jakwings/macdict,和mattt/DictionaryKit。