Iphone 使用coredata fetchrequest预测

Iphone 使用coredata fetchrequest预测,iphone,core-data,nspredicate,Iphone,Core Data,Nspredicate,我正在使用CoreData开发一个应用程序。我有一个简单的结构: 名字 姓氏 我需要更新条目,以便执行以下操作: if (strLastname.lengt > 0) { predicate = [NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND dbLastname LIKE[c] %@)", strName, strLastname]; else { predicate = [NSPredicate predica

我正在使用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];
}
现在的问题是,当my DB包含更多同名用户且其中一个用户没有姓氏时,谓词将始终匹配名称类似于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];
}
谓词为OK,但fetchedObject为空

感谢您的帮助


Max

而不是像try-CONTAINS一样,这为我解决了遇到相同问题时的问题。此外,请正确检查所有拼写,无论您将哪些字段名和变量粘贴在其中,谓词都是正确的。

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

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

我试过喜欢,不喜欢,包含,不包含,它们都不起作用。找到了一种方法。我检查长度。[NSPredicate predicateWithFormat:@“(类似于[c]@的dbName,而不是dbLastname.length>0)”,strName];