I'm not sure if this belongs in Stack Overflow, if it doesn't please let me know.
I have this piece of code that adds contacts to an array, if a contact with that phone/name combination already exists in the array it does not add it again (meaning no duplicates).
It works as intended but it increases build times dramatically and I am looking for a better way to use contains
or some other approach.
var contacts = [CNContact]()
let name: String = contact.givenName + " " + contact.middleName + " " + contact.familyName
if (name.stringByReplacingOccurrencesOfString(" ", withString: "")).length > 1
{
if contact.phoneNumbers.count > 0
{
// Check if contacts already contains name/phone combination
if let phoneNumber: String = (contact.phoneNumbers[0].value as? CNPhoneNumber)?.stringValue
{
if contacts.contains({$0.phoneNumbers.count > 0 && ($0.phoneNumbers[0].value as? CNPhoneNumber)?.stringValue == phoneNumber}) &&
contacts.contains({($0.givenName + " " + $0.middleName + " " + $0.familyName) == name})
{ /* Contact with same name/phone combination already exists in array */ }
else { contacts.append(contact) }
}
}
}