Filter NSArray with NSDictionary (nested level of object) using NSPredicate ANY and IN?

北慕城南 提交于 2020-01-14 06:09:17

问题


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

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.

This is working fine.

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

This is not working.

 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.

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.


回答1:


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'}"]


来源:https://stackoverflow.com/questions/37544681/filter-nsarray-with-nsdictionary-nested-level-of-object-using-nspredicate-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!