代码之家  ›  专栏  ›  技术社区  ›  jorjj

Swift 4:没有这样的模块'CoreServices.DictionaryServices'

  •  0
  • jorjj  · 技术社区  · 7 年前

    示例代码:

    import Foundation
    import CoreServices.DictionaryServices
    
    func define(_ word: String) -> String? {
        let nsstring = word as NSString
        let cfrange = CFRange(location: 0, length: nsstring.length)
    
        guard let definition = DCSCopyTextDefinition(nil, nsstring, cfrange) else {
            return nil
        }
    
        return String(definition.takeUnretainedValue())
    }
    
    define("apple") // "apple | ˈapəl | noun 1 the round fruit of a tree..."
    

    Apple's documentation 但是对于如何实现和使用它却没有任何解释。

    解决方案:

    感谢下面回答我问题的人,我做了一些研究,找到了解决办法。显然,我可以通过调用 UIReferenceLibraryViewController ,但苹果的文档说,我们不应该使用这个制作字典的应用程序。显然,仅仅制作一个字典应用程序是不够的,因为它使用自己的字典应用程序 ViewController 不可自定义。

     let dic = UIReferenceLibraryViewController(term: textLabelOutlet.text as! String)
            dic.modalPresentationStyle = .popover // add this
    
            let popover = dic.popoverPresentationController
            popover?.sourceView = view
            popover?.sourceRect = CGRect(x: 32, y: 32, width: 64, height: 64)
            present(dic, animated: true)
    

    资料来源: UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)

    2 回复  |  直到 7 年前
        1
  •  1
  •   keji    7 年前

    CoreServices DictionaryServices 似乎只在macOS上可用。

    https://developer.apple.com/documentation/coreservices/1446842-dcscopytextdefinition

    仅SDK列表: macOS 10.5+

        2
  •  1
  •   dengApro    7 年前

    CoreServices.DictionaryServices 是OSX的框架,而不是iOS的框架。

    你可以点击 核心服务.DictionaryServices

    /*
        DictionaryServices.h
        DictionaryServices framework
    
    */
    
    /*!
        @typedef    DCSDictionaryRef
        @abstract   Opaque CF object that represents a dictionary file
    */
    public class DCSDictionary {
    }
    
    
    @available(OSX 10.5, *)
    public func DCSGetTermRangeInString(_ dictionary: DCSDictionary?, _ textString: CFString, _ offset: CFIndex) -> CFRange