Home > Back-end >  Pulling DatePicker value from CoreData to display with Text("")
Pulling DatePicker value from CoreData to display with Text("")

Time:09-19

I've gotten all my other textfields and pickers to pull from CoreData and display using Text(""). However, I can't for the life of me figure out how to display the date picked in the date picker using the Text(""). For my other fields I've used this syntax:

Textfields: Text("\(record.hoursSlept, specifier: "%.2f") Hours Slept")

Picker: Text("Mask Type: \(record.maskType ?? "N/A")")

Toggle: if (record.smartStartToggle) == false {Text("Smart Start Off").......

DatePicker: Text("\(record.todaysDate ?? "date error")")

The date picker one just gives an error and I've tried a date formatter and that doesn't work either..... HELP!

CodePudding user response:

try this, where the date is tested for nil value first, then display the String "date error" or the String \(record.todaysDate!)

 Text(record.todaysDate == nil ? "date error" : "\(record.todaysDate!)")

instead of: Text("\(record.todaysDate ?? "date error")") where you have "date error" that is a String, but record.todaysDate is a Date, and so cannot be compared.

  • Related