我已在我的数据模型中的实体listItems
和Person
之间添加了一个名为ListItem
的多对多关系,然后成功将对象添加到给定的{{1}使用
Person
其中 let personSelectedListItems = person.mutableSetValue(forKey: "listItems")
personSelectedListItems.addObjects(from: selectedListItems)
print("personSelectedListItems after addObjects contains \(personSelectedListItems)")
是selectedListItems
类型的对象数组。 print语句给出:
ListItem
这很好,因为我希望为每个personSelectedListItems after addObjects contains Relationship 'listItems' on managed object (0x6080000d2050) <PersonMO: 0x6080000d2050> (entity: Person; id: 0xd000000000040002 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/Person/p1> ; data: {
age = Ky;
firstName = Uykyu;
image = <89504e47 0d0a1a0a 0000000d 49484452 000002cc 00000333 08020000 003d00d3 35000000 01735247 4200aece 1ce90000 001c>;
isVisited = 0;
lastName = Kuyuy;
listItems = (
"0xd000000005140000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p325>",
"0xd000000005680000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p346>",
"0xd0000000023c0000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p143>"
);
locationCity = Uyk;
locationState = You;
notes = "";
phoneNumber = "";
score = nil;
}) with objects {(
<ListItemMO: 0x6000000b6c20> (entity: ListItem; id: 0xd000000005140000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p325> ; data: {
category = Meta;
isSelected = 1;
listItem = "Similar values";
listItemStatus = 1;
listItemWeight = 1;
people = (
"0xd000000000040002 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/Person/p1>"
);
}),
<ListItemMO: 0x6000000af900> (entity: ListItem; id: 0xd000000000080000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p2> ; data: {
category = Appearance;
isSelected = 1;
listItem = Attractive;
listItemStatus = 3;
listItemWeight = 1;
people = (
"0xd000000000040002 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/Person/p1>"
);
}),
<ListItemMO: 0x6000000b27e0> (entity: ListItem; id: 0xd0000000023c0000 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/ListItem/p143> ; data: {
category = Behavior;
isSelected = 1;
listItem = "Good grammar";
listItemStatus = 1;
listItemWeight = 1;
people = (
"0xd000000000040002 <x-coredata://1350DE85-4F65-462A-9C36-1EEE3D5298CD/Person/p1>"
);
})
)}
创建这些对象,但这很糟糕,因为它是一个我看似无法访问的无序集。我希望它是一个可变数组,所以我可以在索引处访问每个对象,但是当我这样做时
Person
我收到此错误消息:
let personSelectedListItems = person.mutableArrayValue(forKey: "listItems")
personSelectedListItems.addObjects(from: selectedListItems)
print("personSelectedListItems after addObjects contains \(personSelectedListItems)")
这令人费解,因为我不知道为什么*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSManagedObjects of entity 'Person' do not support -mutableArrayValueForKey: for the property 'listItems''
得到支持,但阵列版本不是。我真的希望能够通过关系访问对象,理想情况是通过点符号。最好的方法是什么?
答案 0 :(得分:0)
核心数据与多人关系始终由集合表示。对此的部分支持是NSManagedObject
实现mutableSetValue(forKey:)
,这是访问关系的文档化方法之一。由于多对多关系是集合,NSManagedObject
不支持mutableArrayValue(forKey:)
,因为该错误消息表明。
如果您想要有序关系,请在数据模型编辑器中将其标记为已订购,然后您可以使用NSOrderedSet
。它仍然不是一个阵列,但确实有订购。
答案 1 :(得分:0)
@Wain和@TomHarrington都指出了我在这方面的正确方向,但这是我的问题的答案(在经过一些搜索和修补之后):
首先,我需要在我的数据模型文件的数据模型检查器中将多对多关系标记为“有序”。第二件事是改变
let personSelectedListItems = person.mutableSetValue(forKey: "listItems")
personSelectedListItems.addObjects(from: selectedListItems)
print("personSelectedListItems after addObjects contains \(personSelectedListItems)")
到
let personSelectedListItems = person.mutableOrderedSetValue(forKey: "listItems")
personSelectedListItems.addObjects(from: selectedListItems)
print("personSelectedListItems after addObjects contains \(personSelectedListItems)")
这样mutableOrderedSetValue
就会反映出这种关系的有序性。在那之后,我有一个有序的返回集。欢呼!但是现在如何通过关系访问和更改这些单个对象?这让我想到了第三件事 - 我不得不把这个集合转向我的班级(称为ListItemMO
)。要获取有序集中的第一个对象,然后通过关系更改属性,它看起来像这样:
if let listItemSet = personVar.listItems?.object(at: 0) as? ListItemMO {
listItemSet.listItemStatus = 0
}