在尝试更新现有联系人时,我收到sigabrt错误“提取联系人时未请求属性”. var cntct= existingContact.mutableCopy() as! CNMutableContactlet phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566") cntct
var cntct= existingContact.mutableCopy() as! CNMutableContact let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566") cntct.phoneNumbers.append(phone)我一直在与自己的相关问题苦苦挣扎几个小时:我需要为swift的联系人添加生日.通过一些研究,试验和错误以及故障排除,以下是我提出的建议:
var contactStore = CNContactStore() var contactx:CNMutableContact = CNMutableContact() let predicate = CNContact.predicateForContactsMatchingName("\(firstnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)") // searches for contacts matching the inserted name (inputted by the user as first name, then last name, then any suffixes). let toFetch = [CNContactBirthdayKey] do{ var contacts = try contactStore.unifiedContactsMatchingPredicate( predicate, keysToFetch: toFetch) print(contacts) for contact in contacts { let birthday = NSDateComponents() birthday.year = Int(yearfield.text!)! // sets the birthday year birthday.month = Int(monthfield.text!)! // sets the birthday month birthday.day = Int(dayfield.text!)! // sets the birthday day let mutableContact = contact.mutableCopy() as! CNMutableContact mutableContact.birthday = birthday // sets the contacts found with predicate search to having the birthday set above. let saveRequest = CNSaveRequest() saveRequest.updateContact(mutableContact) try contactStore.executeSaveRequest(saveRequest)
显然,这会增加生日而不是电话号码,但您可以使用完全相同的原则(谓词搜索,联系人中的联系人)来添加电话号码;只是改变联系人循环中联系人内部发生的事情!我希望这会对你有所帮助,抱歉你没有早点得到答复.
基本上,您可以更改联系循环内部的内容
let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566") cntct.phoneNumbers.append(phone)
你应该有一个电话号码添加过程.