Core Data中的多对一关系的属性声明
我有一个使用Core Data编写的应用程序。我有2个实体与一对多的关系。我有两个子类NSManagedObject。
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.
播放列表的界面:
@interface VBPlaylist : NSManagedObject {
}
@property (readwrite, copy) NSString *name;
@end
播放列表的实现:
@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?
一对一关系建模为Core Data的对象引用。所以从实体 Bar
到实体 Baz
(假设 Baz
由 Baz
类实现
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
@interface Bar : NSManagedObject {
}
@property (readwrite,nonatomic) Baz * toBaz;
@end
多对多关系被建模为可变集属性(虽然不是 NSMutableSet
)。假设从 Bar
到 Baz
调用 manyBazz
:
To-many relationships are modeled as a mutable set property (though not as an NSMutableSet
). Assuming a to-many relationship from Bar
to Baz
called 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
如果要使用NSMutableSet接口来操作 manyBazz
关系,您应该调用 -mutableSetValueForKey:@manyBazz
获得符合KVO的代理
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.
在Leopard(OS X 10.5)及更高版本中,所有适当的方法都是在运行时自动生成的Core Data框架,即使你没有显式声明或实现它们(当然,如果你试图使用它们,而不在头文件中声明它们,你会得到一个编译器警告)。因此,你不需要子类
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 need to subclass
获取声明和实现的最简单的方法是在数据建模器中选择属性,并选择将Objective-C 2.0方法声明复制到剪贴板从设计 - >数据模型菜单中粘贴到实现类.h文件中。当然,你必须保持你的.h和模型同步...因此,对于rentzsch的真棒 MO Generator ,一种工具,它将从您的数据模型自动生成(并重新生成)NSManagedObject子类。
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.