gpt4 book ai didi

ios - 使用 Core Spotlight 索引内容

转载 作者:可可西里 更新时间:2023-11-01 04:33:14 24 4
gpt4 key购买 nike

在邮件应用程序或消息应用程序中,您可以使用 Core Spotlight 搜索来搜索任何消息的内容。我还可以看到 OneNote 这样做,所以它应该在 API 中可用。

但是,几乎没有相关文档。我只能看到 CSSearchableItemAttributeSet 中有 contentUrl,但我尝试设置 .txt 文件的 NSUrl,但没有任何反应。还尝试将 contentType 设置为 kUTTypeTextkUTTypeUTF8PlainText 但没有改进。

是否需要某种特定的文件格式?或者其他应该做的事情?

最佳答案

Apple documentation on CoreSpotlight分解了创建项目并将其添加到可搜索索引的过程:

  • Create a CSSearchableItemAttributeSet object and specify properties that describe the item you want to index.

  • Create a CSSearchableItem object to represent the item. A CSSearchableItem object has a unique identifier that lets you refer to it later.

  • If needed, specify a domain identifier so that you can gather multiple items together and manage them as a group.

  • Associate the attribute set with the searchable item.

  • Add the searchable item to the index.

这是一个简单的示例 I,它展示了如何索引一个简单的 Note 类:

class Note {
var title: String
var description: String
var image: UIImage?

init(title: String, description: String) {
self.title = title
self.description = description
}
}

然后在其他一些功能中,创建您的笔记,为每个笔记创建一个 CSSearchableItemAttributeSet,从属性集中创建一个唯一的 CSSearchableItem,并索引可搜索项目的集合:

import CoreSpotlight
import MobileCoreServices

// ...

// Build your Notes data source to index
var notes = [Note]()
notes.append(Note(title: "Grocery List", description: "Buy milk, eggs"))
notes.append(Note(title: "Reminder", description: "Soccer practice at 3"))
let parkingReminder = Note(title: "Reminder", description: "Soccer practice at 3")
parkingReminder.image = UIImage(named: "parkingReminder")
notes.append(parkingReminder)

// The array of items that will be indexed by CoreSpotlight
var searchableItems = [CSSearchableItem]()

for note in notes {
// create an attribute set of type Text, since our reminders are text
let searchableItemAttributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)

// If we have an image, add it to the attribute set
if let image = note.image {
searchableItemAttributeSet.thumbnailData = UIImagePNGRepresentation(image)
// you can also use thumbnailURL if your image is coming from a server or the bundle
// searchableItemAttributeSet.thumbnailURL = NSBundle.mainBundle().URLForResource("image", withExtension: "jpg")
}

// set the properties on the item to index
searchableItemAttributeSet.title = note.title
searchableItemAttributeSet.contentDescription = note.description

// Build your keywords
// In this case, I'm tokenizing the title of the note by a space and using the values returned as the keywords
searchableItemAttributeSet.keywords = note.title.componentsSeparatedByString(" ")

// create the searchable item
let searchableItem = CSSearchableItem(uniqueIdentifier: "com.mygreatapp.notes" + ".\(note.title)", domainIdentifier: "notes", attributeSet: searchableItemAttributeSet)
}

// Add our array of searchable items to the Spotlight index
CSSearchableIndex.defaultSearchableIndex().indexSearchableItems(searchableItems) { (error) in
if let error = error {
// handle failure
print(error)
}
}

此示例改编自 AppCoda's How To Use Core Spotlight Framework in iOS 9指导。

关于ios - 使用 Core Spotlight 索引内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38568457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com