使用NSPredicate过滤数组的NSDictionary [英] Filter an NSDictionary of Arrays using NSPredicate

查看:128
本文介绍了使用NSPredicate过滤数组的NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个plist,我已经定义为包含许多数组的Dictionary。我想从字典中删除所有具有与给定字符串匹配的第一个Item 0(默认命名约定)的数组。

I have a plist that I have defined as a Dictionary that contains many arrays. I would like to pull out of that dictionary all arrays that have their first "Item 0" (Default naming convention) that matches a given string.

我尝试过类似的东西过滤NSArray,但我得到一个正则表达式错误,控制台会在字典中输出第一个数组的内容。

I tried something similar to Filtering an NSArray, but I am getting a regex error and the console spews out the contents of the first array in the dictionary.

另外,我是否需要实例化我的NSDictionary作为NSMutableDictionary因为我将对它执行过滤处理?是不是更好的形式将NPDictionary从plist复制到一个必须的,然后对它进行工作?

Also, would I need to instantiate my NSDictionary as an NSMutableDictionary since I will be performing a filter process on it? Is it better form to copy the NSDictionary read in from the plist to a mustable one and then do the work on it?

推荐答案

这是可能的,但是以迂​​回的方式。我建议你考虑使用不同的方法。

It's possible, but in a roundabout way. I recommend that you consider using a different approach.

话虽这么说,你可以这样做:

That being said, here's how you'd do it:

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                   [NSArray arrayWithObjects:@"a", @"b", @"c", nil], @"a",
                   [NSArray arrayWithObjects:@"b", @"c", @"a", nil], @"b",
                   [NSArray arrayWithObjects:@"c", @"a", @"b", nil], @"c",
                   [NSArray arrayWithObjects:@"a", @"b", @"c", nil], @"d",
                   nil];
NSPredicate *p = [NSPredicate predicateWithFormat:@"%@[SELF][0] == 'a'", d];
NSLog(@"%@", p);
NSArray *keys = [d allKeys];
NSArray *filteredKeys = [keys filteredArrayUsingPredicate:p];
NSLog(@"%@", filteredKeys);
NSDictionary *matchingDictionary = [d dictionaryWithValuesForKeys:filteredKeys];
NSLog(@"%@", matchingDictionary);

以下是发生的事情:

我们我们有源字典, d 。这是一个字母,其中有一个字母( a b c ,或 d )键入一个数组。我们将找到对应于第一个元素 a 的数组的所有

We've got our source dictionary, d. This is a dictionary where a letter (a, b, c, or d) is keyed to an array. We're going to find all the keys that correspond to an array where the first element is a.

我们的谓词是:

[NSPredicate predicateWithFormat:@"%@[SELF][0] == 'a'", d];

这里我们在源字典中使用索引运算符。 SELF ,在评估此谓词时,将是 d 之一。所以%@ [SELF] 将返回一个数组。然后我们取该数组的第0个元素并将其与字符串 a 进行比较。如果它匹配,则返回 YES

Here we're using the indexing operator on our source dictionary. SELF, when this predicate is evaluated, will be one of the keys of d. So %@[SELF] will return an array. We then take the 0th element of that array and compare it to the string a. If it matches, then this returns YES.

然后我们获取字典中的所有键并使用它们过滤它们这个谓词。这意味着结果数组将只包含相应数组的第一个元素为 a 的键。

We then grab all the keys in the dictionary and filter them using this predicate. This means that the resulting array will contain only the keys where the first element of the corresponding array is a.

一旦我们有匹配的键,我们从源词典中提取一个子词典来获得我们最终的缩减词典。

Once we have the matching keys, we extract a "sub dictionary" from our source dictionary to get our final, reduced dictionary.

在上面的测试中, matchingDictionary 有两个键( a d ),它们都是键控的包含(a,b,c)的数组。

In the test above, matchingDictionary has two keys (a and d), which are both keyed to an array containing (a, b, c).

如果你想要 NSArray 而是包含匹配的数组,你可以使用 - [NSDictionary objectsForKeys:notFoundMarker:]

If you want an NSArray instead that contains the matching arrays, you'd use -[NSDictionary objectsForKeys:notFoundMarker:] instead.

然而,这是一个尴尬的解决方案,我仍然认为你应该重新组织你的数据。

However, this is an awkward solution, and I still think you should reorganize your data.

这篇关于使用NSPredicate过滤数组的NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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