简体   繁体   中英

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]%@.

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, eg

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM