过滤包含特定关键字的字典数组

时间:2013-10-15 04:26:27

标签: objective-c nsarray nsdictionary nspredicate nssortdescriptor

我正在从服务器检索数据,使用搜索功能。数据是一组字典,如

{
 text : Some simple text,
 searchword : Apple text with new features,
 keyword : Normal words
 }
{
 text : Random,
 searchword : Newly generated text,
 keyword : Normal words
}
{
 text : Some simple word,
 searchword : Latest version,
 keyword : Normal words
}

如果值包含“text”

,我需要过滤掉所有字典中的任何键

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

NSMutableArray *filteredArray = [NSMutableArray array];
for (NSDictionary *dictionary in initialArray) {
    NSMutableDictionary *filteredDictionary = [NSMutableDictionary dictionary];

    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSString *text = (NSString *)obj;

        if ([text rangeOfString:@"text"].location == NSNotFound) {
            [filteredDictionary addObject:obj forKey:key];
        }
    }];

    [filteredArray addObject:filteredDictionary];
}

答案 1 :(得分:0)

只要假设下面是你的字典数组就很简单: -

for (NSDictionary *dc in yourArr)
{
    NSArray *yourArr)=[dc allValues];
    for (i=0; i<[arry count]; i++)
    {
        if ([[yourArr objectAtIndex:i] isEqualToString:@"text"])
        {
            NSLog(@"found");
        }
    }
}