在CSSearchableItemAttributeSet中设置NSarray的title属性

我正在尝试在应用程序中使用CoreSpotlight API,我有plist文件,其中有几个项目,例如动物的名字。 所以我需要设置titlestring等于对这些对象,例如,如果用户search狮子,线名称,例如其function出现在聚光灯下。 这是我的代码:

 - (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.plistsearch特定的单词谢谢。

编辑[Plist图片]: 在这里输入图像说明

您可以维护NSArray并遍历playDictionariesArray,使用数据源中的特定条目创build&初始化CSSearchableItem对象。

 - (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"); } }]; } } 

我没有运行和testing代码。

你没有循环每个键。 使用此问题中提供的代码。

CoreSpotlight索引

在支持索引的设备上试用。 不是iPhone 4 / 4s或iPad。