使用NSPredicate ANY和IN使用NSDictionary(对象的嵌套级别)过滤NSArray? [英] Filter NSArray with NSDictionary (nested level of object) using NSPredicate ANY and IN?

查看:90
本文介绍了使用NSPredicate ANY和IN使用NSDictionary(对象的嵌套级别)过滤NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 originalListArray 的主数组,其中包含多个对象.以下是对象的示例. Example.json

I have one main array named originalListArray with multiple objects.Here is the example of object. Example.json

我想从 originalListArray 中找到该对象的数量,这些对象的amenity_id与我的过滤器数据匹配. 我这样做了,创建一个谓词usign ANY 而不是 self ,因为NSArray使用NSDictionary,并且在Dictionary对象中可能包含NSArray数据.

I want to find that number of objects from the originalListArray, who have amenity_id matched with my filter data. I have do this, create one predicate usign ANY not self becuase NSArray with NSDictionary and in that Dictionary object may have NSArray data.

这很好.

 NSPredicate *predicate=[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id =='1')"];

这不起作用.

 NSPredicate *predicate=[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id in ('1','2','3')"];

因此,可以很容易地过滤单个值,但在不透明的情况下使用它会导致崩溃和错误,就像这样.

So Single value can be filter easily but usign IN oparation its crash and error is like this.

由于未捕获的异常"NSInvalidArgumentException"而终止了应用程序,原因:无法解析格式字符串('1','2','3')中的任何ANY.amenity_id""'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "ANY amenities.amenity_id in ('1','2','3')"'

非常感谢,如果有人可以帮助我的话.

Thanks in advanced, if any one can help me in this.

推荐答案

我认为最好使用%@占位符将数组作为参数传递.

I think it's better to pass the array as a parameter with %@ placeholder.

[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id IN %@", @[@"1",@"2",@"3"]]

这样,您可以手动创建阵列,并且如果需要更改它,则更加容易.

That way, you can create manually your array, and if you need to change it, it's easier.

如果您仍然希望它手动更改字符串格式":

If you still want it to do it manually changing the "string format":

[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id IN {'1', '2', '3'}"]

这篇关于使用NSPredicate ANY和IN使用NSDictionary(对象的嵌套级别)过滤NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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