Search code examples
swiftmacoscocoaxcode9corespotlight

Trying to use CoreSpotlight on OSX


I'm trying to add spotlight integration to my mac app but get error. Here're the steps:

  • I've created a new cocoa app
  • I've added CoreSpotlight and CoreServices frameworks to the "Link binary with libraries" section of my project build settings and picked them as optional
  • Added import CoreSpotlight to the head of my view controller
  • import CoreServices to the head of my view controller

Then I've added the line to the viewDidLoad of my viewController

let _ = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)

Then I run the app, the project crashes with EXC_BAD_ACCESS (code=1, address=0x0)

enter image description here

I've already cleaned the project, deleted derived data, restarted my mac, etc...

I'm running on XCode Version 9.0 (9A235) and MacOX Sierra 10.12.6 (16G29).

Any suggestions may help.

Thanks in advance.


Solution

  • The CoreSpotlight documentation suggests that API was added on macOS in High Sierra (10.13).

    SDKs

    iOS 9.0+

    macOS 10.13+

    I can confirm it works for me on macOS 10.13.

    Since you are running 10.12, the API does not exist.