【发布时间】:2020-04-22 19:48:58
【问题描述】:
我正在尝试开发一个应用程序来将值输入到列表中显示的实体中。但是当我从列表中删除它们时,它们并没有在数据库中删除。你有解决办法吗?
struct ViewList: View {
@Environment(\.managedObjectContext) var context
@State var newName: String = ""
@FetchRequest(
entity: ItProduct.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \ItProduct.name, ascending: true)]
) var list: FetchedResults<ItProduct>
var body: some View {
VStack {
HStack {
TextField("I insert the name of the list", text: $newName)
Button(action: {
if self.newName != "" {self.add()}
}) { Image(systemName: "plus")}
}
List {
ForEach(list, id: \.self) { i in
ViewItem(product: i)
}.onDelete { indexSet in
for index in indexSet {
self.context.delete(self.list[index])
}
}
}
}
}
}
【问题讨论】:
标签: swift xcode core-data swiftui