这不是一个有效的谓词格式字符串,所以即使你最终生成它,你也无法将它转换成NSPredicate
这就是你想要的:
NSDictionary *groupValuePairs = ....;
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *group in groupValuePairs) {
NSArray *values = [groupValuePairs objectForKey:group];
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K IN %@", group, values];
[subpredicates addObject:p];
}
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
这没有您原来暗示的大小写和变音符号不敏感。如果你真的需要它,那么你将需要变得更复杂一些:
NSDictionary *groupValuePairs = ....;
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *group in groupValuePairs) {
NSArray *values = [groupValuePairs objectForKey:group];
NSMutableArray *groupSubpredicates = [NSMutableArray array];
for (NSString *value in values) {
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", group, value];
[groupSubpredicates addObject:p];
}
NSPredicate *p = [NSCompoundPredicate orPredicateWithSubpredicates:groupSubpredicates];
[subpredicates addObject:p];
}
NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];