using NSPredicate with a set of answers
Asked Answered
C

1

6

I have a set of strings containing personIDs and I have a NSFetchedResults of people managedObjects with unique strPersonIDs. I tried to create an NSPredicate but it fails. Any help with this would be greatly appreciated. I'm a bit new to NSPredicate so be kind.

NSSet *zipSet = [NSSet setWithSet:[self getziplist:searchText]];
searchString = [NSString stringWithFormat:@"(strPersonID IN %@)",zipSet];
NSPredicate *searchPersonPredicate = [NSPredicate predicateWithFormat:searchString];

The runtime error message is: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(strPersonID IN {( 300040, 300082, 412218 )})"'

Closefisted answered 11/12, 2010 at 17:41 Comment(0)
M
7

Don't interpolate zipSet into the string, interpolate it into the predicate:

NSSet *zipSet = [NSSet setWithSet:[self getziplist:searchText]];
NSPredicate *searchPersonPredicate = [NSPredicate predicateWithFormat:@"strPersonID IN %@",zipSet];

If you interpolate the NSSet into a string, it won't have the correct format (NSString uses -description, which uses the old NextStep property list format).

Mehitable answered 11/12, 2010 at 18:2 Comment(3)
outis - could you help with an example of this? I'm not sure I know what you mean by interpolate. Thanks in advance.Closefisted
@jangelo42: My answer above already has an example. Take a closer look at the source code snippet. Interpolate and string interpolation are defined online.Mehitable
@jangelo42: if you're curious about the different formats (which illustrate what's invalid and what's valid), add the line that sets searchString from your original code to your current code and use a debugger to inspect the values of searchString and [searchPersonPredicate predicateFormat].Mehitable

© 2022 - 2024 — McMap. All rights reserved.