获取Core Data关系中的对象数 [英] Get number of objects in a Core Data relationship

查看:104
本文介绍了获取Core Data关系中的对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview与食谱。
用户可以添加和删除配方。
当单击一个食谱时,另一个表格视图被按下,并显示成分。
在这里,用户可以添加和删除成分。

I have a tableview with recipe. The user can add and remove recipe. When a recipe is clicked, another tableview is pushed, and display the ingredients. Same here, the user can add and remove ingredients.

食谱和配料之间有oneToMany关系。

There is a oneToMany relationship between recipe and ingredients.

这里是我的挑战:

我想显示配方tableview行中的成分数量。我知道如何设置它的所有在界面生成器与行,但我不知道如何获取一个配方的成分计数。

I want to display the number of ingredient in the recipe tableview row. I know how to set it all up in interface builder with the rows, but I dont know how to get the count of ingredient for a single recipe.

这是可能吗?

提前感谢!

推荐答案

如果您使用核心数据,可以通过调用计数方法询问配料列表中有多少配料:

If you are using core data you can just ask the ingredients list how many ingredients it has inside it by calling the count method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    int count = [recipe.ingredients count];
    return count;
}



您可以通过查看集合类具有的属性和方法文档中的NSSet类

通过查看核心数据创建的生成类,我发现了什么类:

I found out what class to look at by looking at the generated class that core data creates:

@class ParentObject;
@interface ParentObject :  NSManagedObject  
{
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet* childObjects;
@end

这篇关于获取Core Data关系中的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆