使用 NSPredicate 过滤,用于数组内字典内数组的数组计数 [英] Filtering with NSPredicate, for array count of Array inside dictionary inside array

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

问题描述

我有如下格式的数组

<预><代码>[{"xyz" : [不同值的数组];...更多的钥匙},{..和上面的字典一样}...更多词典]

在这里,我有一个主要的字典数组,其中每个字典都有不同的键,其中有xyz"键,其值也是一个数组.现在我想要那些字典,xyz 的数组必须有 count>2.

现在我尝试了以下谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];NSArray *filteredArray = [resultArray filteredArrayUsingPredicate:predicate];

但这给了我这样的错误,xyz 不是键计数"的键值投诉

解决方案

在这种情况下导致 -valueForKey: @"count" 被发送到每个 xyz 实例,而 xyz 不是关键值编码符合计数.

相反,在需要计数时使用 @count 运算符来计算集合的计数,然后使用

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.@count>2"];

代替

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];

I have Array of format as below

[
   {
      "xyz" : [Array with different values];
      ... many more keys            
   },
   {
     .. same as above dictionary
   }
   ... many more dictionaries
]

Here see, i have Main Array of dictionaries, where each dictionary have different keys, in which there is "xyz" key, whose value is again an Array. Now i want those dictionaries for which, xyz's array must have count>2.

Now i have tried with following predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];
NSArray *filteredArray = [resultArray filteredArrayUsingPredicate:predicate];

but this is giving me error something like this, xyz is not key-value complaint for key "count"

解决方案

In this case that results in -valueForKey: @"count" being sent to each xyz instance, and xyz isn't key value coding compliant for count.

Instead, use the @count operator to evaluate the count of the collection when you want the count then use

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.@count>2"];

instead of

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];

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

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