Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using NSPredicate with a set of answers

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

like image 461
jangelo42 Avatar asked Dec 11 '10 17:12

jangelo42


1 Answers

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).

like image 139
outis Avatar answered Oct 07 '22 17:10

outis