在CSSearchableItemAttributeSet中从NSarray设置标题属性 [英] Set title property from NSarray in CSSearchableItemAttributeSet
问题描述
我正在尝试在应用程序中使用 CoreSpotlight
API,我有一个plist文件,上面有几个项目,例如动物的名字.因此,我需要将 title
字符串设置为与那些对象的on相等,例如,如果用户搜索Lion,则行名称及其特征例如出现在聚光灯下.这是我的代码:
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");
}
}];
}
}
问题是 getNames
返回所有名称!!!当用户从 animals.plist
中搜索特定单词时,如何过滤它谢谢 .
The problem is getNames
returns all names !!! how can I filter it when is user is searching an specific word from animals.plist
Thanks .
编辑[列表图像]:
推荐答案
您可以维护NSArray并通过playDictionariesArray进行迭代,从而创建&使用数据源中的特定条目初始化CSSearchableItem对象.
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.
这篇关于在CSSearchableItemAttributeSet中从NSarray设置标题属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!