user6510422 user6510422 - 6 months ago 46
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.


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