macos Core Data 中多对多关系的属性声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/849942/
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
Property declaration for to-many relationships in Core Data
提问by hekevintran
I have an application written using Core Data. I have 2 entities with a one-to-many relationship. I have subclassed NSManagedObject for both of them. The entity on the one-side is called Playlist and the other is called Song.
我有一个使用 Core Data 编写的应用程序。我有 2 个具有一对多关系的实体。我已经为他们两个子类化了 NSManagedObject。一侧的实体称为播放列表,另一侧的实体称为歌曲。
The interface for Playlist:
播放列表界面:
@interface VBPlaylist : NSManagedObject {
}
@property (readwrite, copy) NSString *name;
@end
The implementation for Playlist:
播放列表的实现:
@implementation VBPlaylist
@dynamic name;
@end
I think that I should have another property to indicate the Songs under the Playlist class. I cannot find any sample code that shows to-many relationships written as properties. How do you do this?
我认为我应该有另一个属性来指示播放列表类下的歌曲。我找不到任何显示编写为属性的多对关系的示例代码。你怎么做到这一点?
回答by Barry Wark
To-one relationships are modeled as object references by Core Data. So a to-one relationship from Entity Bar
to entity Baz
(assuming Baz
is implemented by the class Baz
) would be
一对一关系被 Core Data 建模为对象引用。所以从实体Bar
到实体的一对一关系Baz
(假设Baz
是由类实现的Baz
)将是
@interface Bar : NSManagedObject {
}
@property (readwrite,nonatomic) Baz * toBaz;
@end
To-many relationships are modeled as a mutable set property (though notas an NSMutableSet
). Assuming a to-many relationship from Bar
to Baz
called manyBazz
:
对多关系被建模为一个可变的集合属性(虽然不是一个NSMutableSet
)。假设从Bar
到Baz
被调用的一对多关系manyBazz
:
@interface Bar : NSManagedObject {
}
@property (retain) NSSet* manyBazz;
@end
// coalesce these into one @interface AnalysisRecord (CoreDataGeneratedAccessors) section
@interface Bar (CoreDataGeneratedAccessors)
- (void)addManyBazzObject:(Baz *)value;
- (void)removeManyBazzObject:(Baz *)value;
- (void)addManyBazz:(NSSet *)value;
- (void)removeManyBazz:(NSSet *)value;
@end
If you want to use the NSMutableSet interface to manipulate the manyBazz
relationship, you should call -mutableSetValueForKey:@"manyBazz"
to get a KVO-compliant proxy for the manyBazz
relationship.
如果您想使用 NSMutableSet 接口来操作manyBazz
关系,您应该调用-mutableSetValueForKey:@"manyBazz"
以获取该manyBazz
关系的 KVO 兼容代理。
On Leopard (OS X 10.5) and later, all appropriate methods are automaticall generated at run-time by the Core Data framework, even if you do not explicitly declare or implement them (you will, of course, get a compiler warning if you try to use them without declaring them in a header file). Thus you do not needto subclass
在 Leopard (OS X 10.5) 及更高版本上,所有适当的方法都会在运行时由 Core Data 框架自动生成,即使您没有明确声明或实现它们(当然,如果您尝试,您将收到编译器警告使用它们而不在头文件中声明它们)。因此你不需要子类化
The easiest way to get the declaration and implementation right is to select the attributes in the data modeler and choose "Copy Objective-C 2.0 Method Declarations To Clipboard" from the "Design->Data Model" menu, the paste into your implementing classes .h file. Of course, you have to keep your .h and model in sync... hence a hearty recommendation for rentzsch's awesome MO Generator, a tool that will automatically generate (and re-generate) NSManagedObject subclasses from your data model.
获得正确声明和实现的最简单方法是在数据建模器中选择属性,然后从“设计->数据模型”菜单中选择“将 Objective-C 2.0 方法声明复制到剪贴板”,粘贴到您的实现类中。 h文件。当然,你必须让你的 .h 和模型保持同步......因此强烈推荐rentzsch的很棒的MO Generator,这是一个从你的数据模型自动生成(和重新生成) NSManagedObject 子类的工具。
回答by Jim Dovey
The simplest way to create .h and .m files for your CoreData entities is this:
为 CoreData 实体创建 .h 和 .m 文件的最简单方法是:
- Select an entity in the data modeler.
- Press Command-N or select File->New File…
- Select 'Cocoa' from the source list.
- In the template chooser, you should now see an item called 'Managed Object Class'. If this isn't there, click Cancel and repeat steps 1-2.
- Press Next, choose the project/target, and press Next again.
- Now you see something like the following window: New Managed Object Class window http://blog.alanquatermain.net/images/ManagedObjectClass.png
- Select the options you need and click Finish.
- 在数据建模器中选择一个实体。
- 按 Command-N 或选择 File->New File…
- 从源列表中选择“可可”。
- 在模板选择器中,您现在应该看到一个名为“Managed Object Class”的项目。如果不存在,请单击取消并重复步骤 1-2。
- 按 Next,选择项目/目标,然后再次按 Next。
- 现在您会看到类似于以下窗口的内容:New Managed Object Class window http://blog.alanquatermain.net/images/ManagedObjectClass.png
- 选择您需要的选项并单击完成。
This will generate the following header and source files:
这将生成以下头文件和源文件:
Entity.h:
实体.h:
#import <CoreData/CoreData.h>
@interface Entity : NSManagedObject
{
}
@property (retain) NSNumber * uniqueID;
@property (retain) NSString * name;
@property (retain) Entity * parent;
@property (retain) NSSet* children;
@end
@interface Entity (CoreDataGeneratedAccessors)
- (void)addChildrenObject:(Entity *)value;
- (void)removeChildrenObject:(Entity *)value;
- (void)addChildren:(NSSet *)value;
- (void)removeChildren:(NSSet *)value;
@end
Entity.m:
实体.m:
#import "Entity.h"
@implementation Entity
@dynamic uniqueID;
@dynamic name;
@dynamic parent;
@dynamic children;
@end
Note that the implementation doesn't contain anything except @dynamic
markers to tell the compiler not to worry about missing methods/variables to match the properties.
请注意,除了@dynamic
告诉编译器不要担心缺少方法/变量来匹配属性的标记之外,该实现不包含任何内容。
回答by DRVic
This has slightly changed. As of 4.2.1 (March 2012), you go to New File; select Core Data (not Cocoa), and then select New NSObjectModel subclass. From there it produces the subclass as described above.
这略有改变。从 4.2.1(2012 年 3 月)开始,您转到新建文件;选择 Core Data (not Cocoa),然后选择 New NSObjectModel subclass。从那里它产生如上所述的子类。