问题
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]%@.
回答1:
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.
来源:https://stackoverflow.com/questions/14578513/nspredicate-core-data