using NSPredicate with a set of answers

落爺英雄遲暮 提交于 2019-12-01 17:29:35

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/4418027/using-nspredicate-with-a-set-of-answers

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