大佬教程收集整理的这篇文章主要介绍了ios – Swift嵌套过滤器优化?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
CNContact-> phonenumbers->(String,CNPhonenumber-> stringvalue)
在下面的代码中假定我通过以下方式@L_801_5@了联系人:
let keys = [CNContactEmailAddressesKey,CNContactPhonenumbersKey,CNContactFormatter.descriptorForrequiredKeysForStyle(.FullName)] let fetchrequest = CNContactFetchrequest(keysToFetch: keys) var contacts:[CNContact] = [] try! CNContactStore().enumerateContactsWithFetchrequest(fetchrequest) { ...
我想将stringvalue与已知值进行比较.这是我到目前为止在游乐场所拥有的:
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 Contacts框架通过电话号码(或电子邮件地址,相同的想法)找到联系人?
以上是大佬教程为你收集整理的ios – Swift嵌套过滤器优化?全部内容,希望文章能够帮你解决ios – Swift嵌套过滤器优化?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。