Ios 在CSSearchableItemAttributeSet中从NSarray设置标题属性

Ios 在CSSearchableItemAttributeSet中从NSarray设置标题属性,ios,objective-c,iphone,xcode,corespotlight,Ios,Objective C,Iphone,Xcode,Corespotlight,我试图在应用程序中使用corephootlightAPI,我有一个plist文件,上面有一些项目,例如动物的名字。因此,我需要将titlestring设置为这些对象的on,例如,如果用户搜索Lion,那么行名称以及它的特性就会出现在聚光灯下。这是我的密码: - (void)setupCoreSpotlightSearch { CSSearchableItemAttributeSet *attibuteSet = [[CSSearchableItemAttributeSet alloc]

我试图在应用程序中使用
corephootlight
API,我有一个plist文件,上面有一些项目,例如动物的名字。因此,我需要将
title
string设置为这些对象的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
谢谢

编辑[Plist图像]:

您可以维护NSArray并遍历playDictionariesArray,使用数据源中的特定条目创建并初始化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");
            }
       }];  
   }
}

我还没有运行和测试代码。

您没有遍历每个键。使用此问题中提供的代码

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

为什么要删除
attibuteSet.title
??这是我的主要问题,必须有一个valueplayDictionariesArray将包含所有名称的列表。因此,您可以将标题设置为
attributeSet.title=object
。您可以检查编辑。我将对象更改为
for(播放字典数组中的NSString*name)
并设置
attributeSet.title=name
,但问题是我的plist文件中只有最后一项是可搜索的,即
Birds
我还有许多其他动物!然后只需要迭代到最后一个对象,即Birds
for(在[playDictionariesArray objectAtIndex:[playDictionariesArray count]-1]中的NSString*名称)
谢谢!但现在应用程序崩溃了<代码>由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSTaggedPointerString CountByEnumerating with State:objects:count::]:发送到实例的无法识别的选择器您的代码不会调用
NSLog(@“搜索项目索引”)完全正确!好像没什么用。我称之为
自聚光灯索引]
viewDidLoad中。。。但你应该做的是设置一个bool,只运行一次。否则,它将在每次发射时运行。