58

I'm looking for a way to use NSPredicate to set a LIKE condition to fetch objects. In addition to that, an OR would be useful as well. I'm trying to do something where if a user searches "James" I can write an NSPredicate that will do the equivalent of:

select * from users where firstname LIKE '%James%' OR lastname LIKE '%James%';

3 Answers 3

128
NSString *_mySearchKey = @"James";
NSPredicate *_myPredicate = [NSPredicate predicateWithFormat:@"(firstname CONTAINS[cd] %@) OR (lastname CONTAINS[cd] %@)", _mySearchKey, _mySearchKey];
6
  • 37
    Found ! It means Case & Diacritic insensitive Feb 27, 2013 at 13:48
  • 1
    @AlexReynolds why don't you use LIKE ?
    – onmyway133
    Apr 10, 2014 at 14:54
  • A better question is: Why should I use LIKE, instead? Oct 17, 2014 at 0:40
  • this is not working with empty string @"". Any suggestions? [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", strFilter] not working when strFilter is @""
    – Add080bbA
    May 12, 2015 at 11:45
  • Check if the string is empty before running your predicate test, and handle that case separately. stackoverflow.com/questions/899209/… May 12, 2015 at 18:12
38

The CONTAINS operator will certainly work just fine. If you're looking for a more direct correlation, then you can also use the LIKE operator (* = 0 or more characters, ? = 1 character):

NSString *_mySearchKey = @"James";
NSPredicate *_myPredicate = [NSPredicate predicateWithFormat:@"firstname LIKE '*%1$@*' OR lastname LIKE '*%1$@*'", _mySearchKey];

For reference:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215868

5
  • 6
    I know that's a long time ago, but this doesn't work, since NSPredicate doesn't substitute quoted content '%@' will stay '%@'... Apr 3, 2011 at 10:33
  • 1
    beginswith[c] does it for me but still, has anyone managed to generate a dynamic LIKE query such as '%@*' Oct 26, 2012 at 15:45
  • 10
    The trick to using like is to include the * marks in the argument. ie: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE[cd] %@", kMDItemDisplayName, [NSString stringWithFormat:@"*%@*", appname]];
    – Richard H.
    Nov 26, 2013 at 2:22
  • What does the 1$@ do?
    – shim
    Dec 22, 2015 at 22:20
  • @shim identifies specifically the first argument. Contrast with if the author had just used %@ twice; in that case he'd have needed to supply _mySearchKey twice. E.g. NSLog(@"%1$@ %1$@", @"Hat"); will output "Hat Hat".
    – Tommy
    Feb 22, 2017 at 15:31
10

Another possibility

@"firstname beginswith[c] James"

As a nice alternative to contains

Sometimes contains isn't always the right answer

1
  • 1
    Does not match "SuperJames" :) beginswith is not equal to sql's like '%foo%'
    – marsbear
    Apr 30, 2016 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.