带有 coredata fetchrequest 的 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 为空。
任何帮助表示赞赏。
最大限度
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我遇到同样的问题时,这解决了我的问题,而不是像尝试包含一样。另外,请正确检查所有拼写,无论您输入什么字段名称和变量,谓词都是正确的。
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.
找到办法了。我检查长度。
Found a way. I check the length.