带有 coredata fetchrequest 的 nspredicate

发布于 2024-12-03 15:10:32 字数 808 浏览 1 评论 0原文

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

任何帮助表示赞赏。

最大限度

I'm working with an app using CoreData. I have a simple structure:

Name
Lastname

I need to update an entry so I do:

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

now the problem is when my DB contains more user with same name and one of them has no lastname, the predicate will match always the first entry where the name is like strName so I should check if dbLastname is like '' but it fails:

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];
}

the predicate is OK but fetchedObject is empty.

Any help is appreciated.

Max

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-12-10 15:10:32

当我遇到同样的问题时,这解决了我的问题,而不是像尝试包含一样。另外,请正确检查所有拼写,无论您输入什么字段名称和变量,谓词都是正确的。

Instead of LIKE try CONTAINS, this fixed the issue for me when I ran into the same problem. Also, check all the spelling properly, the predicate is correct no matter what field names and variables you stick in there.

音盲 2024-12-10 15:10:32

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

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

Found a way. I check the length.

[NSPredicate predicateWithFormat:@"(dbName LIKE[c] %@ AND not dbLastname.length > 0)", strName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文