I want to use List(data:, editActions:, rowContent:), the data: needs a $property, but I can't pass $persons in there directly. Is there any way to solve like this below:
import SwiftUI
import SwiftData
struct BindableTest: View {
@Environment(\.modelContext) var modelContext
@Query var persons: [Person]
var body: some View {
VStack {
Button("Add") {
let person = Person(name: "test", age: 12)
modelContext.insert(person)
}
List($persons, editActions: .all) { $person in
Text(person.name)
Text(person.age.formatted())
}
}
}
}
I saw this question is similar to my question: https://stackoverflow.com/questions/76464884/swiftdata-list-bindings-with-query, but those answers don't suit my situation.
editActions: .allargument. Add a.onMoveand.onDeleteclosure, which will (re)enable list editing. Add@Bindable var personinside the list closure, e.g. ``` List(persons) { person in @Bindable var person = person } .onDelete { indexSet in } .onMove { indexSet, arg in } ```