Swift 嵌套过滤器优化?

Swift nested filter optimization?

我试图在 Swift 中使用 KVC 做一些在 Objective-C 中很容易的事情。新的Contacts framework added in iOS9 is for the most part easier to use than the old AddressBook API。但是通过其手机 phone 号码查找联系人似乎很困难。为查找联系人提供的谓词仅限于名称和唯一标识符。在 Objective-C 中,您可以获得所有联系人,然后使用 NSPredicate 来过滤 KVC 查询。结构是:

CNContact->phoneNumbers->(String, CNPhoneNumber->stringValue)

在下面的代码中假设我通过以下方式获取联系人:

let keys = [CNContactEmailAddressesKey,CNContactPhoneNumbersKey, CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)]
let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
var contacts:[CNContact] = []
try! CNContactStore().enumerateContactsWithFetchRequest(fetchRequest) { ...

我想将字符串值与已知值进行比较。这是我从游乐场到现在所拥有的:

import UIKit
import Contacts

let JennysPhone    = "111-867-5309"
let SomeOtherPhone = "111-111-2222"
let AndAThirdPhone = "111-222-5309"

let contact1 = CNMutableContact()
contact1.givenName = "Jenny"
let phone1 = CNPhoneNumber(stringValue: JennysPhone)
let phoneLabeled1 = CNLabeledValue(label: CNLabelPhoneNumberMobile, value: phone1)
contact1.phoneNumbers.append(phoneLabeled1)

let contact2 = CNMutableContact()
contact2.givenName = "Billy"
let phone2 = CNPhoneNumber(stringValue: SomeOtherPhone)
let phoneLabeled2 = CNLabeledValue(label: CNLabelPhoneNumberMobile, value: phone2)
contact2.phoneNumbers.append(phoneLabeled2)

let contact3 = CNMutableContact()
contact3.givenName = "Jimmy"
let phone3 = CNPhoneNumber(stringValue: SomeOtherPhone)
let phoneLabeled3 = CNLabeledValue(label: CNLabelPhoneNumberMobile, value: phone3)
contact3.phoneNumbers.append(phoneLabeled3)

let contacts = [contact1, contact2, contact3]

let matches = contacts.filter { (contact) -> Bool in
    let phoneMatches = contact.phoneNumbers.filter({ (labeledValue) -> Bool in
        if let v = labeledValue.value as? CNPhoneNumber
        {
            return v.stringValue == JennysPhone
        }
        return false
    })
    return phoneMatches.count > 0
}

if let jennysNum = matches.first?.givenName
{
    print("I think I found Jenny:  \(jennysNum)")
}
else
{
    print("I could not find Jenny")
}

这确实有效,但效率不高。在设备上,我需要在后台线程中 运行 这个,如果这个人有很多联系人,这可能需要一段时间。有没有更好的方法使用新的 iOS 联系人框架通过 phone 号码(或电子邮件地址,相同的想法)查找联系人?

我猜你想要更 swift-y 的方式,但显然你可以在 Obj-C 中做的任何事情也可以在 swift 中完成。所以,你仍然可以使用 NSPredicate:

let predicate = NSPredicate(format: "ANY phoneNumbers.value.digits CONTAINS %@", "1118675309")
let contactNSArray = contacts as NSArray
let contactsWithJennysPhoneNumber = contactNSArray.filteredArrayUsingPredicate(predicate)

如果您正在寻找更 Swift-y 的方法:

let matches = contacts.filter {
    return [=10=].phoneNumbers
                .flatMap { [=10=].value as? CNPhoneNumber }
                .contains { [=10=].stringValue == JennysPhone }
}

.flatMapphoneNumbers 的每个成员从类型 CNLabeledValue 转换为类型 CNPhoneNumber,忽略那些不能转换的。

.contains 检查这些 phone 号码是否与 Jenny 的号码匹配。