Set title property from NSarray to CSSearchableItemAttributeSet

I am trying to use the CoreSpotlight API in an application, I have a plist file on which there are several elements, for example, the name of the animal. Therefore, I need to set the title string equal to the value of this object, for example, if users search for Lion, the name of the line and, for example, its functions are displayed in the spotlight. Here is my code:

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

Problem getNames returns all names !!! how can I filter it when the user searches for a specific word from animals.plist Thank you.

EDIT [Image of Plist]: enter image description here

+1
source share
2 answers

You can support NSArray and iterate through playDictionariesArray by creating and initializing a CSSearchableItem object with this particular record 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 did not run or test the code.

+1
source

you do not iterate over each key. Use the code provided in this question.

CoreSpotlight Indexing

Try on a device that supports indexing. NOT iPhone 4 / 4s or iPad.

0
source

All Articles