Search code examples
iosswiftcontacts-framework

Delete number phone Contact - Swift 3 - 4


I want to delete a contact number, I use ContactsUI

//only do this to the first contact matching our criteria
   guard let contact = contacts?.first else{
   return
    }
   let editContact = contact.mutableCopy() as! CNMutableContact

   editContact.phoneNumbers[1]..... ? 

in editContact.phoneNumbers[1].. I want to eliminate that number that is in that position

to edit it, I edit it in this way. and it works well

editContact.phoneNumbers[1] =  CNLabeledValue(label: "home",
                                                          value: CNPhoneNumber(stringValue: "1234567"))

but how do I eliminate it


Solution

  • phoneNumbers is an array. Remove the desired element like you would any other array value:

    let editContact = contact.mutableCopy() as! CNMutableContact
    editContact.phoneNumbers.remove(at: 1)
    

    Of course I'd make sure there are at least 2 phone numbers before doing that.