好的,我建立了一个反身关系实体(在我之前发布的{SO问题] 1的帮助下。
我相信我的设置正确:
我有一个实体小组成员。我与自己建立了多对多的关系。
然后我添加一个实体并创建一个“子”实体并将其设置为关系:
GroupMember *groupMember = [NSEntityDescription insertNewObjectForEntityForName:@"GroupMember" inManagedObjectContext:context];
groupMember.name = @"Ted";
GroupMember *subGroupMember = [NSEntityDescription insertNewObjectForEntityForName:@"GroupMember" inManagedObjectContext:context];
subGroupMember.name = @"sub member bill";
[groupMember addSub_member_relObject:subGroupMember];
然后我为任何GroupMember实体发出获取请求,以查看是否已设置关系。我得到的是:
fetch objects: (
"<GroupMember: 0x83cf280> (entity: GroupMember; id: 0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1> ; data: {\n \"member_rel\" = (\n );\n name = Ted;\n \"sub_member_rel\" = (\n \"0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2>\"\n );\n})",
"<GroupMember: 0x83cf4a0> (entity: GroupMember; id: 0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2> ; data: {\n \"member_rel\" = (\n \"0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1>\"\n );\n name = \"sub member bill\";\n \"sub_member_rel\" = (\n );\n})"
)
2012-12-13 12:08:45.352 [9539:14003] name: Ted
2012-12-13 12:08:45.355 [9539:14003] set: Relationship 'sub_member_rel' on managed object (0x83cf280) <GroupMember: 0x83cf280> (entity: GroupMember; id: 0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1> ; data: {
"member_rel" = (
);
name = Ted;
"sub_member_rel" = (
"0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2>"
);
}) with objects {(
<GroupMember: 0x83cf4a0> (entity: GroupMember; id: 0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2> ; data: {
"member_rel" = (
"0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1>"
);
name = "sub member bill";
"sub_member_rel" = (
);
})
)}
A)我不太明白如何阅读此输出。什么是“有对象”部分?是那个有关系的实体? “子成员账单”是否正确设置为子成员关系?
B)当我试图回到groupMember.Sub_member_rel时,我得到一个NSSet。如何迭代这些对象以获取子成员名称(以在表格视图中显示)?
答案 0 :(得分:2)
A)打印NSManagedObject
或其关系的输出通常是冗长且有些复杂。在您的情况下,首先打印关系所属的对象。然后是“with objects”部分,它由特定关系中的对象组成。
B)在To-Many关系中,关系类型始终为NSSet
。与NSArray
一样,您可以像这样迭代NSSet
:
for (GroupMember *subGroupMember in groupMember.sub_member_rel) {
// Do something with managedObject
}