iOS Swift 3 - 删除联系信息字段周年纪念

时间:2016-11-25 10:38:25

标签: swift contact

在Swift 3中是否可以批量删除特定联系人选择中的Anniversary字段?

我找到了向联系人添加周年纪念信息的代码,但我正在寻找代码来删除特定联系人的现有周年纪念信息。

要添加的示例:

//add anniversary
let anniversaryDate = NSDateComponents()
anniversaryDate.month = 6
anniversaryDate.day = 13
let anniversary = CNLabeledValue(label: "Anniversary",
  value: anniversaryDate)
fooBar.dates = [anniversary]

//save
let request = CNSaveRequest()
request.addContact(fooBar, toContainerWithIdentifier: nil)
do{
  try storeo.executeSaveRequest(request)
  print("Successfully stored the contact")
} catch let err{
  print("Failed to save the contact. \(err)")
}

1 个答案:

答案 0 :(得分:0)

您可以过滤日期,然后更新联系人。

let mutableContact = contact.mutableCopy() as! CNMutableContact

let dates = mutableContact.dates.filter({ $0.label != "Anniversary" })

mutableContact.dates = dates

let request = CNSaveRequest()
request.updateContact(mutableContact)

do {

    try contactStore.executeSaveRequest(request)

} catch let error {

    print("Failed to updated contact. \(error)")
}