无法将 'DateComponents' 类型的值分配给 'String?' 类型的值

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

以下代码无法编译,我不确定在哪里可以找到 Apple 文档中的答案:

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

错误源于无法将其转换为字符串的最后一行。

您正在尝试将 dateComponent 值分配给标签。但是标签需要一个字符串值。所以你只需要这样做。

self.ageValueLabel.text = formattedDate