Post

Replies

Boosts

Views

Activity

multidatepicker and saving multiple dates via swiftdata
Any help will be greatly appreciated. Trying to build a calendar/planner app for public school teachers. Classes are held on multiple dates so there is a need for swiftdata to save multiple dates. There are lots of tutorials demonstrating a multidatepicker but none of the tutorials or videos save the dates, via swiftdata. My goal is to save multiple dates. Step 1 is to initialize mockdata; this is done a class called ToDo. var dates:Set = [] Step 2 is the view containing a multidatepicker and other essential code Step 3 is to save multiple dates using swiftdata. Lots of tutorials, code snippets and help using a single date. But after almost 2 weeks of researching youtube tutorials, and google searches, I have not found an answer on how to save multiple dates via swiftdata. Also, I don't know how how to initialize the array of for the mockdata. Here are some code snippets used but the initialization of the array of DateComponenets doesnt work. And saving multiple dates doesn't work either @MainActor @Model class ToDo { var dates:Set<DateComponents> = [] init(dates: Set<DateComponents> = []) { self.dates = dates } } //view struct DetailView: View { @State var dates: Set<DateComponents> = [] @Environment(\.modelContext) var modelContext @State var toDo: ToDo @State private var dates: Set<DateComponents> = [] MultiDatePicker("Dates", selection: $dates) .frame(height: 100) .onAppear() { dates = toDo.dates } Button("Save") { //move data from local variables to ToDo object toDo.dates = dates //save data modelContext.insert(toDo) } } } #Preview { DetailView(toDo: ToDo()) .modelContainer(for: ToDo.self, inMemory: true) }
2
0
335
1w