节省了多对多关系 [英] saving a to-many-relationship

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

问题描述

我开始学习和练习一些CoreData,没有编程经验。



在网上搜索,寻找苹果样本和阅读一些书,



我有两个实体(费用和月份)与称为monthsOfExpense和expensesOfmonth的互惠关系,我在tableView中显示一个月的费用。



我的问题是插入新的费用并保存:我有一个视图,插入新的费用,用户可以插入名称费用,费用的有效期和月份(与monthOfExpense关系相关联,我希望)。



我确定保存费用实体的名称和价值,从字符串中获取字符串和NSDecimal数字。



我的问题是如何将费用关联到一个或几个月?如何保存textField.text作为关系,指明费用属于哪个月?



我从一个textField开始,用户可以插入一个月,假设费用只有一个月有效(用于学习目的和简化)。



我的想法是允许用户选择几个月(使用另一个tableView可选月份)和费用与不同月份的关联。



我知道一个关系作为一个NSSet,但我不能保存属性和



h2_lin>解决方案

确定。在核心数据中,您定义您的费用实体,创建与月实体的关系。现在为这两个实体创建类文件。然后您使用它

 费用* expense1 = [NSEntityDescription insertNewObjectForEntityName:@费用.... 
月* month1 = [context executeFetchRequest:...];
expense1.month = month1;
...
[context save];


I'm starting to learn and practice some CoreData, with no programming experience.

After searching in Web, looking for Apple samples and reading some books, I´m still stucked in one point.

I have two entities (Expenses and Month) with reciprocal relationships called "monthsOfExpense" and "expensesOfmonth" and I'm showing in a tableView the expenses of a single month.

My problem is to insert new expenses and save them: I've a view to insert new expenses where the user can insert the name of Expense, the value and the months that expense is valid (associated with the monthsOfExpense relationship, i hope).

I'm ok saving the name and the value of Expense entity, taking the string and NSDecimal number from the textFields.

My problem is how to associate that expense to a particular or several months? How can I save a textField.text as a relationship, indicating to what month an Expense belongs?

I'm starting with a textField where the user can insert a month, assuming that an Expense is valid for one month only (for learning purposes and simplification).

My idea is to allow the user to select several months (using maybe another tableView with selectable months) and association of an Expense to different months.

I know that a relationship comes as a NSSet, but I'm not being competent to save the attributes and the relationship from a View, at the same time.

Hope was cleared and many thanks in advance for trying to help.

解决方案

OK. In Core data, you define your "Expense" entity, create a relationship to "Month" entity. Now create class files for these 2 entities. Then you use it by

Expense *expense1 = [NSEntityDescription insertNewObjectForEntityName:@"Expense" ....
Month *month1 = [context executeFetchRequest:......];
expense1.month = month1;
...
[context save];

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

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