0
我一直在研究這個問題很久,我錯過了整個足球賽季,沒有看到我心愛的紅人隊幾乎再次失去他們所有的比賽。即使我心愛的阿森納沒有得到我的支持也不太好,所以請幫助:xCode/iOS - 需要幫助製作核心數據搜索謂詞
我正在遍歷一系列核心數據對象。我試圖找到主對象和子對象包含搜索項的對象。如果他們這樣做,我想將它們添加到結果數組中。
有數以百計的垃圾代碼,現在被浪費線,永遠不會被使用,因爲不同的線程,我跟着,並採取虛假線索..
到目前爲止的代碼是這樣的:
NSLog(@"%s", __FUNCTION__);
NSMutableArray *tempArray = [NSMutableArray array];
for (NSManagedObject *object in tableViewModel.items) {
//myPredicate = [NSPredicate predicateWithFormat:@"(people.name contains[cd] %@)", searchText];
//another one for the junk heap
NSLog(@"searchText: %@", searchText); //this is ok
NSLog(@"\n\nobject is: %@\n\n", object); //this is ok
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(people.name contains[cd] %@)", searchText];
BOOL ok = [predicate evaluateWithObject:object]; // this is not ok
if (ok) {
NSLog (@"result is TRUE");
[tempArray addObject:object];
NSLog(@"\n\ntempArray: %@\n\n", tempArray);
} else {
NSLog (@"result is FALSE");
}
}
NSLog(@"out of records");
autoSearchResults = tempArray;
NSLog(@"\n\nautoSearchResults: %@\n\n", autoSearchResults);
return autoSearchResults; // return my own search array
我得到的輸出是什麼如下:
object is: <MainEntity: 0x79c51cb0> (entity: MainEntity; id: 0x79c50880 <x-coredata://99B12EAD-002C-402C-99CA-CF5A328E67E5/MainEntity/p2> ; data: {
audioName = 00010206120609;
audioNo = 0;
date = "2012-02-06 11:10:00 +0000";
Main = "Main-0001 12.02.06";
interpret = nil;
keyword = "<relationship fault: 0x79c708a0 'keyword'>";
order = 0;
people = "<relationship fault: 0x79c708c0 'people'>";
place = "<relationship fault: 0x79c710b0 'place'>";
recurring = 0;
summary = Test1;
symbol = "<relationship fault: 0x79c71110 'symbol'>";
type = "<relationship fault: 0x79c71170 'type'>";
})
2012-02-06 10:00:11.454 Appkeeper[42598:16c03] result is FALSE
我不知道我是否需要使用一個謂語,謂語是否正確。我認爲我沒有正確測試條件。
因此,任何幫助將不勝感激..
編輯 - 添加一點關係模型....
謝謝Mundi ..沒有快樂..我無法得到「object.people」來解決:「在NSManagedObject類型的對象上沒有找到屬性的人」..但是我可以獲得一個字符串值(str1),所以如果re是我可以使用的某種比較,比如「str1包含searchTerm」,那會很棒。 – 2012-02-07 19:40:56
太好了。你的錯誤信息確切地解釋了什麼是錯的。您的數據模型設置不正確。檢查你的xcdatamodeld並確保你的主實體和你想檢查屬性'name'的值的其他實體之間有一個名爲'people'的一對多關係。 – Mundi 2012-02-07 19:50:39
我做..我會添加關係表到我原來的問題...我真的很感謝幫助.. – 2012-02-07 20:28:32