struct ACFTHhistory:查看{ @Environment(.managedObjectContext)var moc @FetchRequest(entity:ACFTScores.entity(),sortDescriptors:[NSSortDescriptor(keyPath:\ ACFTScores.createdAt,ascending:false)])var acftScores:FetchedResults
@State var isPresented = false
@State private var showingDeleteAlert = false
var body: some View {
NavigationView {
List {
Section(header: Text("ACFT History")) {
ForEach(acftScores, id: \.id) { score in
ACFTHistoryView(createdAt: "\(score.createdAt ?? Date())", totalScore: score.totalScore ?? "0")
.foregroundColor(.white)
}.onDelete(perform: delete)
}.alert(isPresented: $showingDeleteAlert) {
Alert(title: Text("Delete Score"), message: Text("Delete Recore Permanently?"), primaryButton: .destructive(Text("Delete")) {
这是我放置@State属性以触发我的有条件的,有效的地方,但是它有问题。
}, secondaryButton: .cancel())
}
}.navigationBarTitle(Text("ACFT Scoreboard"))
.navigationBarItems(trailing: EditButton())
}
}
func delete(at offsets: IndexSet) {
self.showingDeleteAlert = true
我试图通过另一个@State属性设置条件。我能够删除该记录,但它会引发错误并且出现故障。
for index in offsets {
let score = acftScores[index]
moc.delete(score)
do {
try self.moc.save()
} catch {
print(error)
}
}
}
答案 0 :(得分:0)
您可能会考虑添加一个额外的状态值:
@State var offsets: IndexSet?
var body: some View {
NavigationView {
List {
Section(header: Text("ACFT History")) {
ForEach(acftScores, id: \.id) { score in
ACFTHistoryView(createdAt: "\(score.createdAt ?? Date())", totalScore: score.totalScore ?? "0")
.foregroundColor(.white)
}.onDelete (perform: delete)
}
}
.navigationBarTitle(Text("ACFT Scoreboard"))
.navigationBarItems(trailing: EditButton()).alert(isPresented: self.$showingDeleteAlert){
Alert(title: Text("Delete Score"), message: Text("Delete Recore Permanently?"), primaryButton: .destructive(Text("Delete")) {
if let offsets = self.offsets{
for index in offsets {
let score = acftScores[index]
moc.delete(score)
do {
try self.moc.save()
} catch {
print(error)
}
}
}
}, secondaryButton: .cancel())}
}
}
func delete(at offsets: IndexSet) {
self.showingDeleteAlert = true
self.offsets = offsets
}