iOS.Lover
iOS.Lover

Reputation: 6051

Set title property from NSarray in CSSearchableItemAttributeSet

I am trying to using CoreSpotlight API in application , I have plist file which has a several items on it for example animals' name . So I need to set title string equal to on of those object , for example if users search Lion , the line name and for example its features appears on 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");
            }
        }];
    }
}

The problem is getNames returns all names !!! how can I filter it when is user is searching an specific word from animals.plist Thanks .

EDIT [Plist Image]: enter image description here

Upvotes: 1

Views: 360

Answers (2)

George Asda
George Asda

Reputation: 2129

you are not looping through each key. Use the code provided in this question.

CoreSpotlight indexing

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

Upvotes: 0

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

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.

Upvotes: 1

Related Questions