NSPredicate Core Data

4
什么是在Core Data中使用NSPredicate时LIKE [c]%@= [c]%@之间的确切区别?我想要查找一个完全匹配接收器的字符串。 例如:
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];

这个可以用,但我不理解使用LIKE [c]= [c]%@.之间的区别。

1个回答

14

LIKE= 在谓词中的区别在于,LIKE 允许使用 ?* 作为通配符,其中 ? 匹配 1 个字符,* 匹配 0 或多个字符。

都可以通过添加 [c] 来指定不区分大小写。

例子:假设你有一些对象的名字:

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

两者都

[NSPredicate predicateWithFormat:@"name = %@", @"Mark"];
[NSPredicate predicateWithFormat:@"name LIKE %@", @"Mark"];
在文本中查找"Mark",并且同时包含两个

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

查找 "Mark" 和 "mark"。但是只有 LIKE 运算符可以与通配符一起使用,例如:

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

可以匹配到 "Mark","mark","Mike","mike" 和 "M*",但是

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

只找到"M*".

有关更多信息,请参见“谓词编程指南”中的字符串比较


1
一个出色而清晰的答案。我希望你在苹果的文档部门工作... - Mike Gledhill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接