【问题标题】:nspredicate with coredata fetchrequestnspredicate 与 coredata fetchrequest
【发布时间】:2011-12-01 20:54:37
【问题描述】:

我正在使用 CoreData 处理应用程序。我有一个简单的结构:

名称 姓氏

我需要更新一个条目,所以我这样做:

if (strLastname.lengt > 0) {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] %@)", strName, strLastname];
else {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@)", strName];
}

现在的问题是,当我的数据库包含更多具有相同名称的用户并且其中一个没有姓氏时,谓词将始终匹配名称类似于 strName 的第一个条目,因此我应该检查 dbLastname 是否类似于 '' 但它失败:

if (strLastname.lengt > 0) {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] %@)", strName, strLastname];
else {
  predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] '')", strName];
}

谓词正常,但 fetchedObject 为空。

感谢任何帮助。

最大

【问题讨论】:

    标签: iphone core-data nspredicate


    【解决方案1】:

    当我遇到同样的问题时,这不是 LIKE try CONTAINS,而是为我解决了这个问题。另外,请正确检查所有拼写,无论您在其中粘贴什么字段名称和变量,谓词都是正确的。

    【讨论】:

    • 我试过 LIKE,NOT LIKE,CONTAINS,NOTAINS,它们都不起作用。
    • 找到了方法。我cjeck长度。 [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND not dbLastname.length > 0)", strName];
    【解决方案2】:

    找到了方法。我检查长度。

    [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND not dbLastname.length > 0)", strName];
    

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多