のためだけのサービス提供の提案です名前、どのようにしてそれらを見つけるかを要約しましょう。要するに、あなたは述語を使用したい:(。 - 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