如何在一个实体中保存一个以上的人(CoreData) [英] How to save more than one people in an Entity (CoreData)

查看:37
本文介绍了如何在一个实体中保存一个以上的人(CoreData)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的实体:

I have an entity which looks like this:

Entityname = Country

具有城市"和人物"属性.

with the attributes "city" and "person".

Country *country = [NSEntityDescription insertNewObjectForEntityForName:@"Country" inManagedObjectContext:context];


    country.city = @"New York";
    country.person = @"Doug";
    country.person = @"Carry";
    country.person = @"Arthur";

我想在那个城市拯救超过一个.

I want to save more then one people in that City.

我正在使用上面发布的代码,但只保存了最后一个人.

I am using the code posted above, but only the last person is saved.

如何在 CoreData 中节省超过一个?

How I can save more then one people in CoreData?

希望你能帮助我.

推荐答案

解决问题的方法是:

  1. 创建3个实体:国家城市人物
  2. 使用 XCode 的图形工具设置实体的属性(例如 Country.name、City.name、Person.name 等)
  3. 设置实体之间的关系.您需要来自 Country -> City [称之为城市] 的一对多和来自 City -> Person [称之为人] 的一对多(请参阅 Apple 关于此主题的文档).请记住,您还需要设置逆关系.
  4. 这就是所有乐趣的开始... 选择 Editor > Create NSManagedObject subclass.然后 Xcode 将根据您的模型生成文件.现在,如果您查看头文件,您应该会在生成的方法中看到类似于以下内容的内容:
  1. Create 3 Entities: Country, City and Person
  2. Setup the properties for your entities (e.g. Country.name, City.name, Person.name etc) using the graphical tool of XCode
  3. Setup the relations between your Entities. You need a one-to-many from Country -> City [call it cities] and a one-to-many from City -> Person [call it persons] (See Apple's documentation regarding this subject). Keep in mind that you will need to set the inverse relations as well.
  4. That's where all the fun begins... Choose Editor > Create NSManagedObject subclass. Xcode then will generate the files based on your model. Now if you look at the header files, you should see among the generated methods something similar to this:

...

- (void)addPersonObject:(Person *)value;
- (void)removePersonObject:(Person *)value;
- (void)addPersons:(NSSet *)value;
- (void)removePersons:(NSSet *)value;

...

从这一点很明显弄清楚如何添加多个对象:)我知道这一切一开始可能看起来很难,但一旦你深入了解,你将真正能够轻松有效地管理复杂的对象图.我希望这些信息能让您走上正轨!

From this point is quite obvious to figure out how to add multiple objects :) I know that all this may seem hard at first but once you get yourself into this you will really be able to manage complex object graphs easy and efficiently. I hope that this information will set you on the right track!

这篇关于如何在一个实体中保存一个以上的人(CoreData)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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