2017-08-14 16 views
-4

以下代码在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) 
      } 
     } 
    } 
+4

你的函数'retrieveContactsWithStore'是* *里面的'btnContactsTapped'功能。将它移出 – Paulw11

+0

您的缩进看起来很糟糕。可能是事业的一部分。 –

回答

0

功能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) 
    } 
} 
+0

我认为这将是很好的显示@Sravan正确的缩进。 –

+0

@意义重大,请您详细说明缩进中的问题吗? – Sravan

+0

@Sravan他已经固定的缩进它在8月14日编辑。 –

相关问题