简体   繁体   English

将 FetchedRequest/核心数据中的 Date() 加载到日期选择器 SwiftUI

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

Im loading CoreData into my View like this:我像这样将 CoreData 加载到我的视图中:

@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)考虑到我只对某个实体感兴趣,我应用了这个过滤器(只显示一个具有正确 UUID 的实体)

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

This works fine since these TextFields show the correct outputs这很好用,因为这些 TextFields 显示正确的输出

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?变量 "datum"(Type Date(), 也设置为 @State) 是否应该更改为 rechnung.datum(Type Date()) 设置的任何值? and therefore display the date...因此显示日期...

SwiftUI sadly still is documented poorly but maybe Im missing something essential here遗憾的是,SwiftUI 的记录仍然很差,但也许我在这里遗漏了一些重要的东西

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资料来源: https://developer.apple.com/documentation/swiftui/datepicker

Okay I solved the problem by myself, I changed the Picker code to:好的,我自己解决了问题,我将 Picker 代码更改为:

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

which now works, god knows why!现在有效,天知道为什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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