简体   繁体   中英

Load Date() from FetchedRequest/Core Data into Date Picker SwiftUI

Im loading CoreData into my View like this:

@FetchRequest(entity: Rechnungen.entity(), sortDescriptors: []
)var rechnung: FetchedResults<Rechnungen>

Considering im only interested in one certain entity I applied this filter(Only shows a enity which has the right UUID)

ForEach(self.rechnung.filter{
        $0.id == rechnungoffen}) { rechnung in

This works fine since these TextFields show the correct outputs

Form {
        Section(header: Text("Rechnungsdetails")) {
            TextField("\(rechnung.verkaeufer)", text:$verkaeufer)
            TextField("\(rechnung.beschreibung)", text:$beschreibung)

And now I want to show the Date, and here's where Im stuck because this does not work, and I do not really know why

                DatePicker(selection: $datum, in: ...Date()){
                                Text("Datum & Uhrzeit")
                                    .onAppear{
                                        self.datum = rechnung.datum
                                    }

Doesn't the variable "datum"(Type Date(), also set as @State) should change to whatever rechnung.datum(Type Date()) is set to? and therefore display the date...

SwiftUI sadly still is documented poorly but maybe Im missing something essential here

Change the picker code to:

DatePicker(selection: $datum, in: datum...Date()) {
    Text("Datum & Uhrzeit")
        .onAppear{
            self.datum = rechnung.datum
    }
}

Source: https://developer.apple.com/documentation/swiftui/datepicker

Okay I solved the problem by myself, I changed the Picker code to:

DatePicker("Datum & Uhrzeit", selection: $datum, in: datum...Date())
                    .onAppear{
                        datum = rechnung.datum
                    }

which now works, god knows why!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM