Swift 4: No such module 'CoreServices.DictionaryServices'

793 Views Asked by At

I was reading an article that talks about Core Services and decided to use one of them. I import Core Services and add the library on XCode, but it doesn't work.

Sample code:

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..."

It gives a "no such module" error when I import it. I also look at Apple's documentation but there is no explanation for how to implement and use it.

The Solution:

Thanks to people who answered below my question, I made a little research and found the solution. Apparently, I can use the iOS dictionary by calling the UIReferenceLibraryViewController, but the Apple documentation says that we should not use this making for a dictionary app. It is obvious that it is not sufficient to make a dictionary app because it uses its own ViewController and is not customizable.

Here is the sample working code:

 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)

Source: UIReferenceLibraryViewController cannot be presented as popup (always covers full screen)

2

There are 2 best solutions below

0
keji On

CoreServices may be available on all platforms but DictionaryServices seems to be available on macOS only.

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

SDK lists only: macOS 10.5+

2
dengApro On

CoreServices.DictionaryServices is a framework of OS X, not of iOS.

you could click CoreServices.DictionaryServices, and see the page in a mac app project.

/*
    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