gpt4 book ai didi

objective-c - 核心数据和自反关系实体

转载 作者:行者123 更新时间:2023-11-29 11:05:25 24 4
gpt4 key购买 nike

好的,我设置了一个自反关系实体(在我之前发布的 [SO 问题] 的帮助下 1

我相信我设置正确:

enter image description here

我有一个实体组成员。我创建了与自身的多对多关系。

然后我添加一个实体并创建一个“子”实体并将其设置为关系:

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) 我不太明白如何阅读这个输出。什么是“有对象”的部分?是具有关系的实体吗? “子成员(member)账单”是否正确设置为子成员(member)关系?

B) 当我尝试取回 groupMember.Sub_member_rel 时,我得到一个 NSSet。我如何遍历这些对象以取回子成员名称(以显示在 TableView 中)?

最佳答案

A) 打印 NSManagedObject 或其关系的输出通常是冗长且有些复杂的。在您的情况下,首先打印关系所属的对象。然后是“with objects”部分,它由具有特定关系的对象组成。

B) 在一对多关系中,关系类型始终是 NSSet。就像使用 NSArray 一样,您可以像这样遍历 NSSet:

for (GroupMember *subGroupMember in groupMember.sub_member_rel) {
// Do something with managedObject
}

关于objective-c - 核心数据和自反关系实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com