如何更新CNContact?获取CNErrorDomain代码= 2

时间:2017-02-04 15:56:08

标签: ios swift cncontact cncontactstore

我正在尝试更新种姓控制器中的联系人,这些联系人都保存到一定程度,这是在添加CNLabeledValue <CNPhoneNumber>格式的新号码时。我带来了控制台结果,我收到了这个错误

CNErrorDomain Code = 2 "(null)" UserInfo = {CNKeyPaths = (
     phoneNumbers
)},

当我看到如何创建酒店的精髓时,他们没有指标的例子

<CNLabeledValue: 0x1706755c0: identifier = (null), label = iPhone, value = <CNPhoneNumber: 0x170439100: countryCode = ru, digits = + ***** >>

我的代码

  for i in 0..<contactPhoneEditTableViewRowIndex {
        debugPrint("i", i)
        if let editRow = getEditPhoneRow(i) {
            if !editRow.isHidden {
                let phone = editRow.cell.phoneNumberTextField.text ?? ""
                let label = editRow.cell.titlePhoneButton.titleLabel?.text ?? ""
                if phone != "" {
                    let phoneModel = CNLabeledValue<CNPhoneNumber>().settingLabel(label, value: CNPhoneNumber(stringValue: phone))

                    phones.append(phoneModel)
                }
            }
        }
    }

    debugPrint(phones)

    guard let updatedContactModel = contactModel.contact.mutableCopy() as? CNMutableContact else { return }

    updatedContactModel.givenName = first
    updatedContactModel.familyName = last
    updatedContactModel.organizationName = company
    updatedContactModel.phoneNumbers = phones

    contactManager.updateContact(updatedContactModel) { [weak self] (error) in
        if error == nil {
            self?.dismiss(animated: true, completion: nil)
        } else {
            DispatchQueue.main.async {
                SVProgressHUD.showError(withStatus: error!.localizedDescription)
            }
        }
    }

来自ContactManager的此功能

func updateContact(_ contact: CNMutableContact, completion: ((_ error: Error?) -> Void)?) {
    let req = CNSaveRequest()
    req.update(contact)
    let store = CNContactStore()
    do {
        try store.execute(req)
        debugPrint("updateContact success")
        completion?(nil)
    } catch {
        completion?(error)
        let _error = error as NSError
        debugPrint(_error)
    }
}

告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

我更改了此代码

let phoneModel = CNLabeledValue<CNPhoneNumber>().settingLabel(label, value: CNPhoneNumber(stringValue: phone))

let phoneModel = CNLabeledValue(label: phoneLabel, value: CNPhoneNumber(stringValue: phone))它解决了我的问题