你可以使用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»艾茲赫爾·戴克斯特拉
«或枚舉» vikingosegundo
NSString *aString [email protected]"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];