을 단지 서빙 제안의 실제 코드처럼 아무것도 알고 이름, 어떻게하는 사람들을 찾아내는 지 간략히 보겠습니다. 즉, 당신은 술어를 사용하십시오 : (. - try
- 물론, 당신이 do
에 그 포장 줄 어떤 오류가 당신이 원하는 패턴을 처리 catch
구조, 또는)
let predicate = CNContact.predicateForContacts(matchingName: searchString)
let matches = try store.unifiedContacts(matching: predicate, keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]) // use whatever keys you want
을
불행하게도, 당신을 연락처 프레임 워크에서 사용자 정의 술어를 사용할 수 없으며 사전 정의 된 술어 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)
}
}
출처
2017-03-12 16:05:52
Rob
FWIW, 나는이 생각하지 않는다 "너무 광범위." 연락처 프레임 워크 (그리고 그것이 수반하는 어려움)를 알고 있다면이 질문은 분명합니다. – Rob