【问题标题】:NSPredicate Core DataNSPredicate 核心数据
【发布时间】:2013-01-12 18:20:24
【问题描述】:

核心数据NSPredicateLIKE [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]%@. 之间的区别

【问题讨论】:

    标签: iphone ipad core-data nspredicate


    【解决方案1】:

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

    LIKE= 都可以修改为 [c] 以指定不区分大小写。

    示例:假设您有具有名称的对象

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

    两个

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

    找到“马克”,两者都有

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

    找到“标记”和“标记”。但只有LIKE 运算符可以与通配符一起使用,例如

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

    找到“Mark”、“mark”、“Mike”、“mike”和“M*”,但是

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

    只找到“M*”。

    有关详细信息,请参阅“谓词编程指南”中的 String Comparisons

    【讨论】:

    • 一个出色而清晰的答案。我希望你在 Apple 的文档部门工作......
    猜你喜欢
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多