A
A
Alexander2018-02-17 18:26:46
Swift
Alexander, 2018-02-17 18:26:46

Cannot assign value of type 'Date?' to type 'String?' how to overcome?

Cannot assign value of type 'Date?' to type 'String?' <- gives this error next to cell.dateLabel.text = clnt.zakazDate. how to overcome?
It appeared after the core data where the date is stored changed the type from String to Date

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Cherny, 2018-02-17
@sasha1802

You need to do the formatting, use the DateFormatter:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/mm/yyyy"
cell.dateLabel.text =  dateFormatter.string(from: clnt.zakazDate)

usually make an extention for Date in a project to use in different places in one line:
extension Date {
    func dayMonthYearFormat() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/mm/yyyy"
        return dateFormatter.string(from: self)
    }
}

// usage:
ell.dateLabel.text = clnt.zakazDate.dayMonthYearFormat()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question