NSPredicate Core Data

☆樱花仙子☆ 提交于 2019-12-05 02:58:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!