您可以使用 NSPredicate 的子类NSCompoundPredicate
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:
@[[NSPredicate predicateWithFormat:@"SELF.name CONTAINS[cd] %@", aString],
[NSPredicate predicateWithFormat:@"SELF.city CONTAINS[cd] %@", aString],
[NSPredicate predicateWithFormat:@"SELF.state CONTAINS[cd] %@", aString]
]
];
但
«两个以上 - 使用 for» Edsger W. Dijkstra
«或枚举» vikingosegundo
NSString *aString =@"Ney York";
NSMutableArray *predicates = [@[] mutableCopy];
[@[@"name", @"city", @"state"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *format = [NSString stringWithFormat:@"SELF.%@ CONTAINS[cd] %%@", obj];
[predicates addObject:[NSPredicate predicateWithFormat:format, aString]];
}];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];