gpt4 book ai didi

ios - 如何使用 OK 警报按钮删除重复的联系人?

转载 作者:行者123 更新时间:2023-11-28 06:00:40 24 4
gpt4 key购买 nike

当点击“确定”警报按钮时,如何从我的表格 View 中删除重复的联系人?

这是我找到的 Duplicate Contacts()

@objc fileprivate func findDuplicateContacts() {
let keys = [CNContactIdentifierKey as CNKeyDescriptor, CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]
let request = CNContactFetchRequest(keysToFetch: keys)
var contactsByName = [String: [CNContact]]()

do {
try self.contactStore.enumerateContacts(with: request) { contact, stop in
guard let name = CNContactFormatter.string(from: contact, style: .fullName) else { return }
contactsByName[name] = (contactsByName[name] ?? []) + [contact] // or in Swift 4, `contactsByName[name, default: []].append(contact)`

}
} catch let err {
print("error:", err)
}

let duplicates = contactsByName.filter { $1.count > 1 }
let alert = UIAlertController(title: "Alert", message: "Number of duplicates: \(duplicates.count)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in
//HERE I WANT TO REMOVE DUPLICATES
print("you have pressed the ok button")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
print(duplicates)
self.tableView.reloadData()

}

提前感谢您的回复

最佳答案

你应该使用CNSaveRequestfunc delete(_ contact: CNMutableContact)方法,用func execute(_ saveRequest: CNSaveRequest)方法执行CNContactStore

此示例删除所有其他联系人并仅保留一个(位置 0),但您可以添加一个方法来确定哪个联系人更完整并保留那个

完整代码

alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in
print("you have pressed the ok button")
var arrayOfContactsRequests : [CNSaveRequest] = []
for dict in duplicates {
for (index,contact) in dict.value.enumerated() {
if(index != 0) {
let saveRequest = CNSaveRequest()
saveRequest.delete(contact.mutableCopy() as! CNMutableContact)
arrayOfContactsRequests.append(saveRequest)
}
}
}
debugPrint(duplicates)
for request in arrayOfContactsRequests {
do{
try self.contactStore.execute(request)
}
catch let err {
print("error:", err)
}
}
}))

在这个答案的帮助下,这个答案是可能的 How to convert CNContact to CNMutableContact?

关于ios - 如何使用 OK 警报按钮删除重复的联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49875631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com