0
votes

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
[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id IN %@", @[@"1",@"2",@"3"]]? - Larme
@Larme Yes its working. First of all, thank you for your quick response, but I didn't understand why I have to pass as an array. Because IN query we are generally pass as a comma separated value as a string. - Ravi B
If you log the working predicate, you'll see that it's in fact @"ANY amenities.amenity_id IN {'1', '2', '3'}" (using bracelets instead of parenthesis). Not, passing it with a placeholder (%@) gives more easily changes if needed. - Larme
@Larme Ok Thanks. :) - Ravi B

1 Answers

2
votes

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