gpt4 book ai didi

Swift Using Contacts Framework,搜索使用Identifier进行匹配

转载 作者:可可西里 更新时间:2023-11-01 00:57:30 31 4
gpt4 key购买 nike

尝试获取提供联系人标识符的联系人匹配失败。我希望返回联系人,然后使用与之关联的图像。我得到一个零匹配。谢谢你。这段代码是我从演示中获得的,我对编程有点陌生

import Contacts

var contact = CNContact()
var contactStore = CNContactStore()

let foundContact = getContactFromID("94AAD3B1-E9E1-48C9-A796-F7EC1014230A")

func getContactFromID(contactID: String) -> CNContact {

AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {

let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]

var contacts = [CNContact]()
var message: String!

let contactsStore = AppDelegate.getAppDelegate().contactStore

do {
contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)

if contacts.count == 0 {
message = "No contacts were found matching the given name."
}
}
catch {
message = "Unable to fetch contacts."
}


if message != nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Utility.showAlert(nil, message: message)
})
} else {

dispatch_async(dispatch_get_main_queue(), { () -> Void in

self.contact = contacts[0]

print("self.contact: \(self.contact)")

})
}
}
}

return self.contact
}

最佳答案

我解决了 :),我删除了 dispatch_async 的东西,现在可以工作了:这里是固定代码。

func getContactFromID(contactID: String) -> CNContact {

AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
if accessGranted {

let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])

let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]

var contacts = [CNContact]()
var message: String!

let contactsStore = AppDelegate.getAppDelegate().contactStore

do {
contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)

if contacts.count == 0 {
message = "No contacts were found matching the given name."
}
}
catch {
message = "Unable to fetch contacts."
}

self.contact = contacts[0]
}
}
return self.contact
}

关于Swift Using Contacts Framework,搜索使用Identifier进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393384/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com