Bigbigga
Bigbigga

Reputation: 47

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

Upvotes: 0

Views: 454

Answers (2)

Bigbigga
Bigbigga

Reputation: 47

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!

Upvotes: 0

Marcio
Marcio

Reputation: 2117

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

Upvotes: 1

Related Questions