2012-11-29 103 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'“'。引号内的格式说明符未被展开。 –