2014-09-06 42 views
0

我正在研究一個允許用戶將數據添加到表視圖的核心數據應用程序。每個對象都有三個不同的屬性。在將屬性輸入到文本字段後,用戶然後保存數據對象;他們可以在桌面視圖中看到它們。帶有多個文本字段的NS謂詞

我也實現了一個搜索功能,讓用戶使用NSPredicate根據它們的屬性來搜索它們的對象。我查看了this指南和tutorial以瞭解如何執行此操作的信息。每個屬性對應於下面代碼中顯示的謂詞。

我的問題是用戶只能根據第一個屬性搜索對象。因此,當他們在文本字段中輸入第一個屬性的內容時,搜索將返回正確的結果,並且每個屬性都顯示在文本字段中。如果用戶嘗試使用第二個或第三個屬性進行搜索,則不會返回任何內容。

NSPredicate *predAttOne = 
[NSPredicate predicateWithFormat:@"(name CONTAINS %@)", 
_one.text]; 
[request setPredicate:predAttOne]; 


//////////////PROBLEM CODE/////////////// 

//If the code below is commented out, the user is able to search using 
//the first attribute successfully, and al the data attributes are shown 
//in the text fields. If it's not commented out, no data is found. 

NSPredicate *predAttTwo = 
[NSPredicate predicateWithFormat:@"(mint CONTAINS %@)", 
_two.text]; 
[request setPredicate:predAttTwo]; 

NSPredicate *predAttThree = 
[NSPredicate predicateWithFormat:@"(year CONTAINS %@)", 
_three.text]; 
[request setPredicate:predAttThree]; 

有誰知道我可以如何使用謂詞來搜索基於不同的屬性?我知道複合謂詞,但我不確定這就是我需要的這種情況。

非常感謝!

+0

使用所有三個,並結合的結果。 – CrimsonChris 2014-09-06 01:40:30

+0

嗨@CrimsonChris,我不完全確定你的意思;我將如何結合結果? – narner 2014-09-06 02:34:16

回答

1

你可以做謂語是這樣的:

[NSPredicate predicateWithFormat:@"(name CONTAINS %@) OR (mint CONTAINS %@) OR (year CONTAINS %@)", _two.text, _two.text, _two.text]; 
+0

第一個建議完美工作。謝謝! – narner 2014-09-06 02:49:58

+0

我刪除了第二個。這不是有效的謂詞格式。 – Hokage 2014-09-06 02:50:41

+0

啊! @Allan,我只是注意到了一些東西,現在如果我嘗試搜索不存在的東西,它會返回當前存儲的每個對象。 – narner 2014-09-06 02:54:23