在 Swift 3 中是否可以批量删除特定联系人选择中的周年纪念字段?
我找到了向联系人添加周年纪念信息的代码,但我正在寻找从特定联系人中删除现有周年纪念信息的代码。
要添加的示例:
//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)")
}
最佳答案
您可以过滤日期,然后更新联系人。
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)")
}
关于swift - iOS Swift 3 - 删除周年纪念日联系信息字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40802989/