user6510422 user6510422 - 1 year ago 82
Swift Question

Cannot assign a value of type 'DateComponents' to a value of type 'String?'

The following code doesn't compile and I'm not sure where to find the answers within Apple's documentation:

let birthdate = try currentHealthStore.dateOfBirthComponents()
var formatter = DateFormatter()
formatter.dateFormat = "dd-MM"
var formattedDate = formatter.string(for: birthdate)
self.ageValueLabel.text = birthdate

The error stems from the final line where it can't be converted to a string.

Answer Source

You are trying to assign a dateComponent value to label. But a label expects a String value. So yo just havr to do this.

self.ageValueLabel.text = formattedDate
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download