小编Peg*_*lon的帖子

如何使用CSCus​​tomAttributeKey从Spotlight获取自定义值

我试图从Core Spotlight获取一些数据,我使用自定义属性键存储.在macOS和iOS上测试过,结果总是一样的.

我的考试班:

import CoreSpotlight

class SpotlightSearch {
  let domainId = "com.company.some"
  let originalDataKeyName: String

  init() {
    self.originalDataKeyName = domainId.replacingOccurrences(of: ".", with: "_") + "_originalData"
  }

  func addToIndex(title: String, content: String) {
    guard let originalDataKey = CSCustomAttributeKey(keyName: originalDataKeyName, searchable: false, searchableByDefault: false, unique: false, multiValued: false)

      else { return }

    let uniqueId = "MyUniqueId" + title
    let originalContent = NSString(string: content)

    let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
    attributeSet.title = title
    attributeSet.setValue(originalContent, forCustomKey: originalDataKey)
    let item = CSSearchableItem(uniqueIdentifier: uniqueId, domainIdentifier: …
Run Code Online (Sandbox Code Playgroud)

macos ios swift corespotlight

6
推荐指数
1
解决办法
328
查看次数

标签 统计

corespotlight ×1

ios ×1

macos ×1

swift ×1