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

我有以下格式的数组

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

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

现在我尝试了以下谓词:

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

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

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

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

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

代替

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

在Swift 3.0中:

 let fileterdArray = resultArray.filtered(using: NSPredicate.init(format: "xyz.@count>%d", 2))