类型''中没有成员''的值快速
问题描述:
以下代码在self.retrieveContactsWithStore(store: store)
行中给出错误value of type 'ViewController' has no member 'retrieveContactsWitStore'
,与使用self
关键字无关。我提到了很多类似的问题,但我没有得到任何解决方案和解释。任何人都可以解释为什么它给这个错误,以及如何克服这个错误?类型''中没有成员''的值快速
@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
必须外的@IBAction
@IBAction func btnContactsTapped()
{
let store = CNContactStore()
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
store.requestAccess(for: .contacts, completionHandler: { (authorized, error) in
if authorized {
self.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 {
print("Contacts: \(contacts)")
}
} catch {
print(error)
}
}
你的函数'retrieveContactsWithStore'是* *里面的'btnContactsTapped'功能。将它移出 – Paulw11
您的缩进看起来很糟糕。可能是事业的一部分。 –