xcode CoreData 添加对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5370650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:34:56  来源:igfitidea点击:

CoreData adding relationships to-many

iphoneobjective-cxcodecore-data

提问by James Dunay

In coredata I can work with one Entity fairly well, mostly because its nicely documented on the internet. However when i get to relationships i keep finding the same data over and over that tells me how to add one to an entity and and best practices but falls short when giving me actual useable examples.

在 coredata 中,我可以很好地处理一个实体,主要是因为它在互联网上有很好的记录。然而,当我建立关系时,我会一遍又一遍地找到相同的数据,这些数据告诉我如何将一个添加到实体和最佳实践,但在给我实际可用的示例时却达不到要求。

So heres the thing, I've got a one-to-many (between entities:Name and ErgTimes) relationship set up and Im wondering how to add multiple objects to Times for every name. Inside my Name.m file i have

事情就是这样,我建立了一对多(实体:名称和 ErgTimes 之间)关系,我想知道如何为每个名称向 Times 添加多个对象。在我的 Name.m 文件中,我有

- (void)addTimesObject:(ErgTimes *)value;

but i dont know where i should use this to add in times.

但我不知道我应该在哪里使用它来添加时间。

Sorry for the lack of code in this example, but if anyone could just point me to a tutorial that shows the use of relationships so i can get an idea that would be so awesome.

很抱歉在这个例子中缺少代码,但是如果有人可以给我指出一个展示关系使用的教程,那么我就可以得到一个非常棒的想法。

-James.

-詹姆士。

回答by Caleb

Read Custom To-Many Relationship Accessor Methods. You can use the standard -mutableSetValueForKey: method to access your relationship:

阅读自定义对多关系访问器方法。您可以使用标准的 -mutableSetValueForKey: 方法来访问您的关系:

NSMutableSet *ergTimes = [person mutableSetValueForKey:@"ergTimes"];
[ergTimes addObject:newErgTime];

Or, if you need to do the above in a number of places, you might want to implement an accessor for the 'ergTimes' property so that you can add a time directly:

或者,如果您需要在多个地方执行上述操作,您可能需要为 'ergTimes' 属性实现一个访问器,以便您可以直接添加时间:

[person addErgTimesObject:newErgTime];

Examples are given in the section I cited above.

我在上面引用的部分中给出了示例。