gpt4 book ai didi

ios - NSPredicate 嵌套或子查询?

转载 作者:行者123 更新时间:2023-11-29 13:01:10 25 4
gpt4 key购买 nike

我正在尝试编写一个 NSPredicate,它将返回给定食谱的所有成分。我的实体 Recipe 有一个 recipeName,所以我想指定 recipeName,然后 Recipe 与 IngredientList 有关系,IngredientList 与 Ingredients 有一对多关系。我想获取指定配方的所有 Ingredient.ingredientNames。

这是我的数据模型。 screenshot我已经尝试过类似的方法,但它无法编译,而且我确信我的 for 循环有问题:

    NSManagedObjectContext *context = [[self appDelegate] managedObjectContext];

// Construct a fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe"
inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recipeName==%@", targetRecipe.recipeName];

[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
NSError *error = nil;
self.theRecipeArray = [context executeFetchRequest:fetchRequest error:&error];

NSLog(@"The recipe you found was %@", [theRecipeArray objectAtIndex:0]);

//Query the one Recipe for all ingredients?
for (ingredient.IngredientName * Ingredient.ingredientName in theRecipeArray)
[ingredientsArray addObject: ingredientName];

会给我正确的食谱,但我如何获取它的配料列表及其所有配料??

最佳答案

Recipe *recipe = [theRecipeArray objectAtIndex:0];

是您找到的食谱。相关成分很简单

NSArray *ingredients = [recipe.ingredientList.ingredient allObjects];

(allObjects 仅在获取 NSArray 而不是 NSSet 时是必需的。)然后你通过键值编码得到一个包含所有名字的数组:

NSArray *ingredientNames = [ingredients valueForKey:@"ingredientName"];

关于ios - NSPredicate 嵌套或子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com