致任何或所有, 我正在寻求有关 Swift CoreData 某些功能的帮助,特别是在将数据保存到具有“多对多”关系的两个实体时。
我已经搜索过这个网站并且还注册了三个不同的 uDemy 类(class),但我正在努力使我用于教育的应用程序按设计工作。
我已经建立了关系…… ChildEntity - toParent & ParentEntity - toChild…这两个都是“to Many”
我将数据保存到 CoreData 的功能只是保存子项,而不是父项,更具体地说,不是将父项链接到子项。
我知道关系 (toParent) 是 NSSet 类型,但显然,这是不可变的。我在控制台中没有收到任何错误。
我的印象是您可以使用关系名称(即 toParent)将数据保存并链接到不同的实体,但同样,很难看到父项的每个属性和值对是如何通过关系保存的。
@IBAction func saveChild(_ sender: UIBarButtonItem) {
let child = ChildEntity(context: moc)
if let firstName = txtFirstNameChild.text {
child.firstName = firstName
}
if let lastName = txtLastNameChild.text {
child.lastName = lastName
}
child.toParent?.setValue(txtFirstNameMum.text!, forKey: "firstNameMum")
child.toParent?.setValue(txtMobileMum.text!, forKey: “mobileMum")
child.toParent?.setValue(txtFirstNameDad.text!, forKey: "firstNameDad”)
child.toParent?.setValue(txtMobileDad.text!, forKey: “mobileDad”)
ad.saveContext()
}
由于这是我的第一个在线问题,如果您需要更多信息来解释我的症结所在,请告诉我。谢谢
最佳答案
您需要创建父对象或将现有对象指定为父对象,它不会自动存在。
关于ios - Swift CoreData,通过多对多关系保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44126151/