I want to allow a user to remove elements( eg CNPhoneNumber
, CNEmailAddresses
) from a contact after displaying the original contact and depending on what they choose I remove from my edited contact.
Even though I have a mutable contact and use mutable array from the key in my code the returned edited contact won't delete the element.
What am I doing wrong here?
Here is my generic function(Builds and runs fine except for issue listed above)
private func removeFromEditedContact<T:NSString>(labeledValueType:T, with key:String,from contact:CNContact,to toContact:CNContact, at indexPath:IndexPath) -> CNContact {
let mutableContact = toContact.mutableCopy() as! CNMutableContact
//what detail are we seraching for in the contact
if let searchingArray = contact.mutableArrayValue(forKey: key) as? [CNLabeledValue<T>] {
let searchValue = searchingArray[indexPath.row].value
//if detail is present in our mutable contact remove it
var labeledValueToChange = mutableContact.mutableArrayValue(forKey: key) as? [CNLabeledValue<T>]
if let index = labeledValueToChange?.index(where: {$0.value == searchValue}) {
labeledValueToChange?.remove(at: index)
}
}
return mutableContact
}