Вопрос:

I keep getting the error 'Cannot convert value of type 'UITextField?' to expected argument type'Date'

ios swift4

300 просмотра

1 ответ

118 Репутация автора

I am trying to get the persons "age" filled in a text field from user input birthdate (text field) and user Calendar.

    var startDate = birthDate
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-YYYY"
    let currentDate = Date()
    let userCalandar = Calendar.current
    let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .month, .year])
    var age = Calendar.current.dateComponents(components, from: startDate, to: currentDate) (error is here)

Thanks for your thoughts

Andrew

Автор: Drewgost Источник Размещён: 08.11.2017 10:11

Ответы (1)


0 плюса

148 Репутация автора

Решение

This should do the job.

        let textInput = birthDate.text
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-YYYY"
        let startDate = dateFormatter.date(from: textInput!)
        let currentDate = Date()
        let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .month, .year])
        let age = Calendar.current.dateComponents(components, from: startDate!, to: currentDate)
Автор: schtipoun Размещён: 08.11.2017 11:37
Вопросы из категории :
32x32