开发者

NSPredicate multiple field search

开发者 https://www.devze.com 2023-03-09 09:06 出处:网络
I\'m trying to search multiple fields. Something like this: [NSPredicate predicateWithFormat:@\"(name title contains[cd] %@) A开发者_StackOverflowND (title contains[cd] %@\",self.searchBar.text];

I'm trying to search multiple fields. Something like this:

[NSPredicate predicateWithFormat:@"(name title contains[cd] %@) A开发者_StackOverflowND (title contains[cd] %@",  self.searchBar.text];

A search is made on both the name field or the title field.

Also, if anyone knows what a wildcard search would look like I'd appreciate that too.

I tried:

 [NSPredicate predicateWithFormat:@"* contains[cd] %@",  self.searchBar.text];

my error code:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(name contains[cd] %@) OR (title contains[cd] %@"'


First off, you must specify all the properties to examine, since there is no fixed or universal list of properties, and in Objective-C there is no real semantic distinction between a property and any other method.

Second off, to examine multiple properties to see if they contain a search string, you should use OR, not AND, since your search is satisfied if any of the properties match, not all.

Otherwise, the syntax you have appears correct (I'm assuming name title in the first subpredicate is meant to be just name.)


Not sure if anyone still reads this post, but I found your problem. You are missing a closing ")" in the second predicate. Here is the correct code:

[NSPredicate predicateWithFormat:@"(name title contains[cd] %@) AND (title contains[cd] %@)",  self.searchBar.text];

Edit: After testing this I noticed that two format specifiers worked fine with ONE format argument (see code above). When I added a third predicate and format specifier (with only one format argument) it crashed. Either way, you should always have the same amount of format specifiers and format arguments.

0

精彩评论

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