gpt4 book ai didi

Swift - 核心数据中的一对多关系

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

我刚刚开始学习 Swift,来自 Java 和 .NET,它真的很不同,我想使用 Core Data 创建一个小应用程序,我有两个表:Clinic 和 Doctor,诊所可以有一个表或更多医生,所以我需要一对多的关系,这就是我到目前为止所做的:

enter image description here

关系:

enter image description here

enter image description here

我已将 DoctorId 和 ClinicId 添加为 Clinic 表中的索引,并将 DoctorId 添加为 Doctor 表中的索引。我的问题是,此时我是否告诉 Swift 关系是 Clinic.doctorId == Doctor.doctorId 并且 Clinic 表可以有多个 Doctor?我希望看到一个可以告诉 Swift 哪些属性用作链接的地方。我错过了什么吗?我将感谢您的指导。

问候。

最佳答案

您不使用任何其他属性来分配关系 - 该关系独立于任何其他项目而存在。此外,您的关系名称是反向的 - 在 Clinic 上,它应该命名为 doctors,在 Doctor 上,它应该命名为 clinic。要分配关系,请执行以下操作:

let doctorOne = Doctor(...)
let doctorTwo = Doctor(...)
let clinic = Clinic(...)
//assign either way - first way is more logical and makes more sense (especially as Core Data relationships are default optional)
doctorOne.clinic = clinic
doctorTwo.clinic = clinic
//or
clinic.doctors = Set<doctorOne, doctorTwo>

关于Swift - 核心数据中的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380381/

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