使用NSPredicate进行过滤,用于数组内部数组的数组计数

Meh*_*kar 5 objective-c nspredicate ios

我有以下格式的数组

[
   {
      "xyz" : [Array with different values];
      ... many more keys            
   },
   {
     .. same as above dictionary
   }
   ... many more dictionaries
]
Run Code Online (Sandbox Code Playgroud)

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

现在我尝试了以下谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];
NSArray *filteredArray = [resultArray filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)

但这给了我这样的错误, xyz is not key-value complaint for key "count"

Anb*_*hik 6

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

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

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.@count>2"];
Run Code Online (Sandbox Code Playgroud)

代替

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];
Run Code Online (Sandbox Code Playgroud)