Reputation: 7936
I was wondering if there is a way to simplify an NSPredicate that takes in a single query string for multiple comparison targets. I'm searching multiple attributes of a core data entity for the same query string. My current query looks something like this...
[NSPredicate predicateWithFormat:@"(attributeA contains[cd] %@) OR (attributeB contains[cd] %@) OR (attributeC contains[cd] %@)", searchString, searchString, searchString];
Note that this works perfectly, but it does look a bit unsightly. Especially the searchString, searchString, searchString
part. Any tips on how I could possibly simplify this would be great!
thanks!
Upvotes: 3
Views: 4035
Reputation: 1808
You can use NSCompoundPredicate for your OR & AND operations like this.
Obj-C - OR
// OR Condition //
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"X == 1"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"X == 2"];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate1, predicate2]];
Obj-C - AND
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"X == 1"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"X == 2"];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2]];
Swift - OR
let predicate1:NSPredicate = NSPredicate(format: "X == 1")
let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
let predicate:NSPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicate1,predicate2] )
Swift - AND
let predicate1:NSPredicate = NSPredicate(format: "X == 1")
let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
let predicate:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1,predicate2] )
Swift 3 - OR
let predicate1 = NSPredicate(format: "X == 1")
let predicate2 = NSPredicate(format: "Y == 2")
let predicateCompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2])
Swift 3 - AND
let predicate1 = NSPredicate(format: "X == 1")
let predicate2 = NSPredicate(format: "Y == 2")
let predicateCompound = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1,predicate2])
Upvotes: 4
Reputation: 243146
You could do:
NSPredicate *p = [NSPredicate predicateWithFormat:@"attributeA contains[cd] $A OR attributeB contains[cd] $A or attributeC contains[cd] $A"];
NSDictionary *sub = [NSDictionary dictionaryWithObject:searchString forKey:@"A"];
p = [p predicateWithSubstitutionVariables:sub];
Or you could do something weirder, like this:
- (NSPredicate *)buildOrPredicate:(NSDictionary *)stuff {
NSMutableArray *subs = [NSMutableArray array];
for (NSString *key in stuff) {
NSString *value = [stuff objectForKey:stuff];
NSPredicate *sub = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", key, value];
[subs addObject:sub];
}
return [NSCompoundPredicate orPredicateWithSubpredicates:subs];
}
And then invoke that with:
NSDictionary *stuff = [NSDictionary dictionaryWithObjectsAndKeys:
searchString, @"attributeA",
searchString, @"attributeB",
searchString, @"attributeC",
nil];
NSPredicate *p = [self buildOrPredicate:stuff];
The only other thing I can think of that might work is to try using positional specifies in the predicate format. However, I don't know if the parser recognizes them the same way that +stringWithFormat:
does:
NSPredicate *p = [NSPredicate predicateWithFormat:@"attributeA contains[cd] %1$@ OR attributeB contains[cd] %1$@ or attributeC contains[cd] %1$@", searchString];
Upvotes: 3