2011-09-09 55 views
0

我正在使用使用CoreData的應用程序。我有一個簡單的結構:nspredicate with coredata fetchrequest

名稱 姓

我需要更新的項目,所以我做的:

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是空的。

任何幫助表示讚賞。

最大

回答

0

找到了一個方法。我檢查了長度。

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

代替LIKE試載,這個固定的問題,對我來說,當我碰到了同樣的問題。此外,請正確檢查所有拼寫,無論您在那裏粘貼哪些字段名稱和變量,謂詞都是正確的。

+0

我試過像,不喜歡,包含,不包含,他們都沒有工作。 – masgar

+0

找到了一個方法。我看到了這個長度。 [NSPredicate predicateWithFormat:@「(dbName LIKE [c]%@ AND not dbLastname.length> 0)」,strName]; – masgar