在最新的iOS9联系人框架中,如何仅检索具有有效电子邮件地址的CNContact?
当前代码:
func getContacts() -> [CNContact] { let contactStore = CNContactStore() let predicate: NSPredicate = NSPredicate(format: "") let keysToFetch = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactEmailAddressesKey] do { return try contactStore.unifiedContactsMatchingPredicate(predicate,keysToFetch: keysToFetch) } catch { return [] } }
目前(iOS 9.0)似乎没有谓词(
see CNContact Predicates)可用于通过电子邮件地址过滤联系人!
原文链接:https://www.f2er.com/swift/320009.html您不能编写自定义谓词来过滤联系人,正如文档所说:
“请注意,Contacts框架不支持通用谓词和复合谓词”
但是当然你可以“手动”完成它,我给你看一个使用快速枚举的例子:
let contactStore = CNContactStore() fetchRequest.unifyResults = true //True should be the default option do { try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey,CNContactEmailAddressesKey])) { (contact,cursor) -> Void in if (!contact.emailAddresses.isEmpty){ //Add to your array } } } catch{ print("Handle the error please") }