nspredicate与coredata fetchrequest [英] nspredicate with coredata fetchrequest

查看:102
本文介绍了nspredicate与coredata fetchrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

名称
姓氏

我需要更新一个条目do:

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

现在的问题是,当我的数据库包含更多的用户同名,他们没有姓氏,谓词将匹配总是第一个条目,其名称像strName所以我应该检查dbLastname是否像'但它失败:

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

谓词可以,但fetchedObject为空。

the predicate is OK but fetchedObject is empty.

任何帮助。

最大

推荐答案

找到办法。我检查长度。

Found a way. I check the length.

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

这篇关于nspredicate与coredata fetchrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆