swiftcontacts-framework

Store custom label and date in Contact


I'm trying to store a custom label and associated date in a Contact. This is my code:

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

The resulting error (on the last line) is:

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

Any help would be appreciated.


Solution

  • The dates property takes an array of CNLabeledValue<NSDateComponents>.

    You need to adjust your code just a bit in the last line to use 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))