nredredise与coredata fetchrequest

时间:2011-09-09 11:19:44

标签: iphone core-data nspredicate

我正在使用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为空。

感谢任何帮助。

最高

2 个答案:

答案 0 :(得分:0)

而不是LIKE尝试CONTAINS,当我遇到同样的问题时,这解决了我的问题。另外,正确检查所有拼写,无论你在哪个字段名称和变量,谓词都是正确的。

答案 1 :(得分:0)

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

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