开发者

NSPredicate for multi-word search

In my iOS application I have a really simple predicate for my fetch controller.

NSString *format = [NSString stringWithFormat:@"name like[c] '%@'", nameVar];
NSPredicate *predicate = [NSPredicate predicateWithFormat:format];
[fetchController setPredicate:predicate];

It performs basic case-insensitive name lookup. Now I'd like to change it so that I could put a number of words in search box (nameVar has the value from search box) separated by spaces and have the predicate filter the results matching all those keywords.

So if I have two names: "John Smi开发者_JAVA百科th" and "Mary Smith" and I search for: "Smith M" I would like to have only one result but a search like that: "Sm th ith" should return both values.

Does anyone have an idea how should this be implemented?


edit back on a regular computer...

So there are a couple things to be aware of:

  1. You don't need to put quotes around the substitution placeholder in your format string. When the method is building the predicate, it's going to be creating an abstract syntax tree of NSExpression and NSPredicate (specifically NSComparisonPredicate and NSCompoundPredicate) objects. Your string will be placed into an NSExpression of type NSConstantValueExpressionType, meaning that it's already going to be interpreted as a regular string. Placing the single quotes in the format string will, in fact, make your predicate non-functional.
  2. You're not limited to a single comparison in a predicate. From the sounds of it, you want to have as many comparisons as you have "words" in your search string (nameVar). In that case, we'll break the nameVar up into its constituent words, and create a comparison for each word. Once we've done that, we AND them together to create a single overarching predicate. The code below does exactly that.

original answer

You can do this by building your own NSCompoundPredicate:

NSString *nameVar = ...; //ex: smith m
NSArray *names = ...; //ex: John Smith, Mary Smith

NSArray *terms = [nameVar componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *subpredicates = [NSMutableArray array];

for(NSString *term in terms) {
  if([term length] == 0) { continue; }
  NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@", term];
  [subpredicates addObject:p];
}

NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
[fetchController setPredicate:filter];

Warning: typed in a browser on my iPhone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜