[SOLVED] Pulling DatePicker value from CoreData to display with Text(“”)

Issue

This Content is from Stack Overflow. Question asked by dano9258

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!



Solution

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.


This Question was asked in StackOverflow by dano9258 and Answered by workingdog support Ukraine It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?