I am making an attempt to make use of SwiftData to retailer person knowledge. Within the code under I merely wish to show how most of the mannequin factor is inserted. It really works completely if I’ve “AddView” as a perform which return a view. Nevertheless, it would not work when I’ve it as a struct. The counter is all the time zero on this case. May you please have a look?
struct Check: View {
@Setting(.modelContext) personal var modelContext
@Question var customOptions: [CustomTaskOptions]
@Setting(.dismiss) personal var dismiss
var physique: some View {
VStack {
Textual content("rely (customOptions.rely)")
NavigationLink(
vacation spot: AddView().modelContainer(for: CustomTaskOptions.self)) {
addButton()
}
}
}
@ViewBuilder
personal func addButton() -> some View {
ZStack {
Rectangle()
.foregroundColor(.black)
Textual content("+")
.foregroundColor(.white)
}
.padding()
}
}
struct AddView: View {
@Setting(.modelContext) personal var modelContext
@Setting(.dismiss) personal var dismiss
var physique: some View {
Button(motion: {
modelContext.insert(CustomTaskOptions())
dismiss()
}) {
Textual content("add")
}
}
}