0

I am trying to amend a contact in my IOS app but get type error Value of type '[CNContact]' has no member 'familyName'

    let updateContact = try contactStore.unifiedContacts(matching: predicate,keysToFetch: toFetch as [CNKeyDescriptor])
            updateContact.familyName.append(CNLabeledValue(label: CNLabelWork, value: contact["2"]!))

Clearly i have the wrong type for updateContact but can't figure where I have gone wrong

Jeremy
  • 145
  • 1
  • 2
  • 13

1 Answers1

2

The properties that you want to fetch for the CNContacts objects returned by the call to unifiedContacts is specified in the toFetch array. Did you specify the familyName value as part of the toFetch array?

If you are not sure, please provide the code that you used to set up the toFetch array.

Update: The call to unifiedContacts returns an array of CNContact objects. That's why you were getting an error. You'd have to get a CNContact instance out of the array and then modify it. Something like this:

if let updateContact = try contactStore.unifiedContacts(matching: predicate,keysToFetch: toFetch as [CNKeyDescriptor]).first {
    let name = updateContact.familyName + contact["2"]!
    updateContact.setValue(name, forKey:CNContactFamilyNameKey)
}
Fahim
  • 3,466
  • 1
  • 12
  • 18
  • let predicate = CNContact.predicateForContacts(matchingName: delCont) let toFetch = [CNContactGivenNameKey] + [CNContactFamilyNameKey] – Jeremy Mar 21 '17 at 09:27
  • @Jeremy If you can provide the full project, I can debug this easier to see what is going on :) – Fahim Mar 21 '17 at 09:33
  • OK, I missed this the first time around - the call to `unifiedContacts` returns an array of `CNContact` objects. That's why you were getting an error. You'd have to get a `CNContact` instance out of the array and then modify it. – Fahim Mar 21 '17 at 09:43
  • +1 The answer is in the update: `unifiedContacts` returns an array of contacts (obvious really when you think about it) – JeremyP Mar 21 '17 at 09:51
  • Could you please mark it as "correct" if the code I provided works for you? – Fahim Mar 21 '17 at 09:53
  • Fahim My problem still is how to update other elements like organisation phone numbers, email - i.e. overwriting existing values rather than adding a new record\ – Jeremy Mar 21 '17 at 13:35
  • @Jeremy Did you see my code above where the Family Name value is updated? That's how you'd set values for other properties on a CNContact object as well. Basically, the following - `updateContact.setValue(name, forKey:CNContactFamilyNameKey)` – Fahim Mar 21 '17 at 13:52
  • Thanks once again - I did see the code and it works fine for names but my issue is understanding the update of the items like email phone number i.e. the ones with multiple values. Weill leave it for awhile and then have another try – Jeremy Mar 21 '17 at 16:28
  • @Jeremy Phone numbers, URLs, e-mail addresses etc. come as arrays for the `CNContact` object. So, if you know the value you want to replace, you'd have to loop through the array, find the object with the value you want, and then set the new value for that object. If you don't know the exact value but have a new list of values, you'd simply replace the old list with the new list or create a combined list of both sets of values - depends on what you want. – Fahim Mar 22 '17 at 01:02