I have a view for editing time information。The view has two properties startTime and endTime wrapped using @binding, of type Date?
@Binding var startTime: Date?
@Binding var endTime: Date?
Different components are displayed depending on whether they have a value or not, the logic is the same for both.
if startTime == nil {
// something
} else {
DatePicker("Start Time", selection: Binding(
get: { startTime! },
set: { startTime = $0 }
))
}
if endTime == nil {
// something
} else {
DatePicker("End Time", selection: Binding(
get: { endTime! },
set: { endTime = $0 }
))
}
Execute the initData method when the view appears.
private func initData() {
print("Start Time: \(String(describing: startTime))")
print("End Time: \(String(describing: endTime))")
}
The output of the open view is as follows:
Start Time: Optional(2024-10-11 08:07:35 +0000)
End Time: Optional(2024-10-11 08:08:35 +0000)
Screenshot of the interface display:
The end time is displayed correctly in the view that opens, and the start time shows the current time.
The data seen via DEBUG is consistent with the console output, there is just a problem with the display. I don't make any changes and just click the save button and the data is still ‘2024-10-11 08:07:35 +0000’.
I hope to get some answers, thanks!