我正在尝试在应用程序中使用CoreSpotlight
API,我有plist文件,其上有几个项目,例如动物'名称 。因此,我需要将title
字符串设置为等于这些对象的on,例如,如果用户搜索Lion,则行名称及其功能将出现在聚光灯下。这是我的代码:
- (void)setupCoreSpotlightSearch
{
CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"animals" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSString *getNames = [NSString stringWithFormat:@"%@",playDictionariesArray];
NSLog(@"%@",getNames) ;
attibuteSet.title =getNames;
attibuteSet.contentDescription = @"blah blah ";
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"app name"
domainIdentifier:@"com.compont.appname"
attributeSet:attibuteSet];
if (item) {
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:@[item] completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Search item indexed");
}
}];
}
}
问题是getNames
返回所有名字!当用户从animals.plist
搜索特定单词时,如何对其进行过滤
谢谢 。
答案 0 :(得分:1)
You can maintain NSArray and iterate through playDictionariesArray, creating & initialising CSSearchableItem object with that particular entry in your data source.
- (void)setupCoreSpotlightSearch
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"animals" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSMutableArray * searchableItems = [[NSMutableArray alloc]init];
for(object in playDictionariesArray)
{
CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc] initWithItemContentType:(__bridge NSString *)kUTTypeImage];
attibuteSet.title =[NSString stringWithFormat:@"%@",object]; //retrive title from object and add here
//attibuteSet.contentDescription = @"blah blah "; // retrieve description from object and add here
CSSearchableItem *item = [[CSSearchableItem alloc] initWithUniqueIdentifier:@"app name"
domainIdentifier:@"com.compont.appname"
attributeSet:attibuteSet];
[searchableItems addObject:item];
}
if (searchableItems) {
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:searchableItems completionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Search item indexed");
}
}];
}
}
I haven't ran and tested the code.
答案 1 :(得分:0)