2012-11-29 98 views
1

我正在使用核心數據處理食譜書。這是我第一次使用CoreData,目前運行良好,但在iPad的分割視圖中使用它時遇到了一些麻煩。CoreData:基於關係的NSPredicate

這是我的對象模型: http://i621.photobucket.com/albums/tt295/Naosu_FFXI/ObjectModel.png

在我的應用程序,步驟和成分示於在詳細視圖中的兩個表。當我有一個配方,它按預期工作。但是,無論您選擇哪種配方,兩個表的NSFetchedResultsControllers都會提取所有信息。所以使用NSPredicate似乎是最明顯的選擇。

這裏是我的NSPredicate代碼片段:

filteredIngredientsArray = [[NSMutableArray alloc] init]; 
.... snip .... 

//---------- Filtering the ingredients by name ---------- 
NSError *error = nil; 
NSPredicate *ingredientsPredicate = [NSPredicate predicateWithFormat:@"recipe.recipeName == '%@'", selectedName]; 
[fetchRequest setPredicate:ingredientsPredicate]; 
NSLog(@"Filtering the INGREDIENTS with this: %@", selectedName); 

NSArray *loadedIngredients = [_managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
filteredIngredientsArray = [[NSMutableArray alloc] initWithArray:loadedIngredients]; 

[self.ingredientTable reloadData]; 

當我用這個,我的表沒有得到填補期間....所以它的明確的工作,但沒有工作,因爲我希望它。我的NSLog顯示selectedName變量的值是用戶點擊的配方的名稱。

這一直在推動我的牆,它真的困擾我,所以任何反饋/幫助將不勝感激。

回答

0

於斷言格式刪除周圍%@單引號:

[NSPredicate predicateWithFormat:@"recipe.recipeName == %@", selectedName] 
+0

上帝我愛我如何通過簡單的修復...感謝一聲我的頭靠在牆上! 只是一個新手問題:那麼單撇號究竟做了什麼呢?我應該使用它們嗎? – Naosuu

+0

@Naosuu:你會用引號與文字字符串進行比較:''recipe.recipeName =='apple pie'「'。引號內的格式說明符未被展開。 –