gpt4 book ai didi

swift - CoreData 关系循环?

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

我有 2 个实体 BuildingsGroupOfBuildings:

Building1 有:GroupOfBuildings1
Building2 有:GroupOfBuildings1

GroupOfBuildings1 有:Building1Building2Building3

关系:

Building <<----> GroupOfBuildings
Building <<--->> GroupOfBuildings
Building <---->> GroupOfBuildings

relationships


问题:

GroupOfBuildings 有一组 Buildings 可以在 Buildings 中使用,还包含一个 mainBuildings 引用建筑物。最重要的是,Buildings 需要对 GroupOfBuildings 的引用。这是建立关系的正确方法还是有另一种方法?似乎设置多个(循环)关系不是我应该做的事情。有什么建议吗?


示例代码:

let buildingDescription = NSEntityDescription.entity(forEntityName: "Buildings", in: context)!
let building1 = Buildings(entity: buildingDescription, insertInto: context)
let building2 = Buildings(entity: buildingDescription, insertInto: context)
let building3 = Buildings(entity: buildingDescription, insertInto: context)

let groupOfBuildingDescription = NSEntityDescription.entity(forEntityName: "GroupOfBuildings", in: context)!
let groupOfBuildings1 = GroupOfBuildings(entity: groupOfBuildingDescription, insertInto: context)

groupOfBuildings1.mainBuilding = building1
groupOfBuildings1.groupOfBuildings = Set([building2, building3])

building1.groupOfBuildings = groupOfBuildings1
building2.groupOfBuildings = groupOfBuildings1

注意事项:

我有另一个问题使用相同的数据,但问题不同。

最佳答案

由于命名错误,这很难理解。称呼事物本来的样子,不要将对象名称复数化。 Buildings 应该叫 BuildingGroupOfBuildings 可能是 BuildingGroup,不知道“items”指的是什么...

也就是说,在相同对象之间建立多重关系是没有问题的。您可以使用 buildingGroup.mainBuildingbuildingGroup.otherBuildingsbuildingGroup.extraBuildings 一切正常,只需正确命名它们即可。

关于swift - CoreData 关系循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348485/

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