基于多个键,从NSArray中过滤整个NSDictionaries [英] Filter entire NSDictionaries out of NSArray based on multiple keys

查看:107
本文介绍了基于多个键,从NSArray中过滤整个NSDictionaries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSArray的NSDictionary对象,我想要能够返回一个新的NSDictionaries数组,其中每个NSDictionary有Area == North(例如)。

I have an NSArray of NSDictionary objects which I would like to be able to return a new array of NSDictionaries from, where every NSDictionary has "Area == North" (for example).

到目前为止,我发现的最近的例子是使用NSPredicate基于NSDictionary键过滤NSArray 但是这只返回给定键的唯一值,而不是具有该键的字典。有没有办法执行类似的操作,并返回整个字典?

The closest example I have found so far is Using NSPredicate to filter an NSArray based on NSDictionary keys but this just returns the unique values for a given key, not the dictionary that has that key. Is there any way to perform a similar operation, and to return the entire dictionary?

推荐答案

听起来很容易:

NSArray *unfilteredDictionaries;  // however you get this...
NSMutableArray *filteredDictionaries = 
  [NSMutableArray arrayWithCapacity:[unfilteredDictionaries count]];
NSDictionary *dict;
for (dict in unfilteredDictionaries)
   if ([[dict valueForKey:@"Area"] isEqualToString:@"North"])
     [filteredDictionaries addObject:dict];

return filteredDictionaries;

这篇关于基于多个键,从NSArray中过滤整个NSDictionaries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆