How to get NOT contacts named "John" with Swift 3

Is there a way to get contacts using the Contact Structure without an attribute?

Example:

myContactArray = unifiedContactsNotCalled("John")

PS: I know that this line does not look like real code, it is just a service offer for illustrative purposes.

-3
source share
1 answer

Before I describe how to find those that do not match the name, let me know how to find those that do. In short, you would use the predicate:

let predicate = CNContact.predicateForContacts(matchingName: searchString)
let matches = try store.unifiedContacts(matching: predicate, keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)])    // use whatever keys you want

(Obviously, you wrapped this in a construct do- try- catchor whatever error handling pattern you want.)

, Contacts, CNContact. , , "", enumerateContacts(with:) :

let formatter = CNContactFormatter()
formatter.style = .fullName

let request = CNContactFetchRequest(keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)])   // include whatever other keys you may need

// find those contacts that do not contain the search string

var matches = [CNContact]()
try store.enumerateContacts(with: request) { contact, stop in
    if !(formatter.string(from: contact)?.localizedCaseInsensitiveContains(searchString) ?? false) {
        matches.append(contact)
    }
}
+2

Source: https://habr.com/ru/post/1651480/


All Articles