在联系人中存储自定义标签和日期

Store custom label and date in Contact

我正在尝试在联系人中存储自定义标签和关联日期。这是我的代码:

let contact = CNMutableContact()
let customLabel = "Label"
let customDate = DateComponents(year:1980, month:1, day:1)
contact.dates.append(CNLabeledValue<DateComponents>(label:customLabel, value:customDate))

产生的错误(在最后一行)是:

"Type 'DateComponents' does not conform to protocol 'NSCopying'"

如有任何帮助,我们将不胜感激。

dates 属性 接受一个 CNLabeledValue<NSDateComponents> 的数组。

您需要在最后一行稍微调整一下代码才能使用 NSDateComponents:

let contact = CNMutableContact()
let customLabel = "Label"
let customDate = DateComponents(year:1980, month:1, day:1)
contact.dates.append(CNLabeledValue<NSDateComponents>(label:customLabel, value:customDate as NSDateComponents))