self
キーワードを使用するかどうかにかかわらず、次のコードではがself.retrieveContactsWithStore(store: store)
行にエラーとなります。私は多くの同様の質問に言及しましたが、私は実用的な解決策と説明を得ていませんでした。誰もがなぜこのエラーを与えているのか、これを克服する方法を説明できますか?タイプ ''のメンバーの値は即時にあります
@IBAction func btnContactsTapped() {
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts, completionHandler: { (authorized: Bool, error: Error?) -> Void in
if authorized {
retrieveContactsWithStore(store: store)
}
})
} else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
self.retrieveContactsWithStore(store: store)
}
func retrieveContactsWithStore(store: CNContactStore) {
do {
let groups = try store.groups(matching: nil)
let predicate = CNContact.predicateForContactsInGroup(withIdentifier: groups[0].identifier)
//let predicate = CNContact.predicateForContactsMatchingName("John")
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey] as [Any]
let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
// self.objects = contacts
DispatchQueue.main.async(execute: {() -> Void in
print("Contacts: \(contacts)")
})
} catch {
print(error)
}
}
}
の外でなければならないあなたの関数 'retrieveContactsWithStore'は' btnContactsTapped'機能*内側*です。それを移動してください – Paulw11
あなたのインデントが悪く見えます。原因の一部になる可能性があります。 –