【发布时间】:2022-01-25 08:19:10
【问题描述】:
如果一个已存在的类别已存在,我如何保存待办事项并设置其与现有类别的关系?我知道如何创建一个新类别并将其与新 Todo 的关系设置为如下所示。我在表单中执行此操作。
Category 设置为具有many Todos。
let category2 = Category(context: viewContext)
category2.id = UUID()
category2.title = "Fun"
let todo = Todo(context: viewContext)
todo.title = "Hi"
todo.dueDate = Date.now
todo.relationship = category2
try? viewContext.save()
//todo.relationship = create a new Category if one doesn't exist, if it does exist, select that as the relationship. If no category, then do nothing
我一直在关注这个解决方案https://www.youtube.com/watch?v=y1oWprQqLJY&t=483s,它也有同样的问题,每次按下按钮时都会创建一个新的“Origin”。
【问题讨论】:
-
您使用 (NS)FetchRequest 对象获取现有类别对象并使用其中之一
标签: ios swift core-data swiftui