Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate Core Data

What is the exact difference between LIKE [c]%@ and = [c]%@ in core data NSPredicate? I want to search for a string that should exactly match the receiver. Example :

NSArray *arrayNames = [context fetchObjectsForEntityName:NSStringFromClass([PatientFamilyMember class])
      withSortColumn:nil withSortDescending:FALSE
      withPredicate:@"patientID = %@ && firstName=[c]%@ && relationship=[c]%@ && lastName=[c]%@",
      self.pfm.patientID, firstName, relationship, lastName];

This works but I have not understood the difference between using LIKE [c] and = [c]%@.

like image 439
sjashish Avatar asked Oct 22 '25 01:10

sjashish


1 Answers

The difference between LIKE and = in a predicate is that LIKE allows ? and * as wildcard characters, where ? matches 1 character and * matches 0 or more characters.

Both LIKE and = can be modified with [c] to specify case insensitivity.

Example: Assume that you have objects with names

"Mark", "mark", "Mike", "mike", "M*"

Both

[NSPredicate predicateWithFormat:@"name = %@", @"Mark"];
[NSPredicate predicateWithFormat:@"name LIKE %@", @"Mark"];

find "Mark", and both

[NSPredicate predicateWithFormat:@"name =[c] %@", @"Mark"];
[NSPredicate predicateWithFormat:@"name LIKE[c] %@", @"Mark"];

find "Mark" and "mark". But only the LIKE operator can be used with wildcards, e.g.

[NSPredicate predicateWithFormat:@"name LIKE[c] %@", @"M*"];

finds "Mark", "mark", "Mike", "mike" and "M*", but

[NSPredicate predicateWithFormat:@"name ==[c] %@", @"M*"];

finds only "M*".

See String Comparisons in the "Predicate Programming Guide" for more information.

like image 67
Martin R Avatar answered Oct 23 '25 17:10

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!