2017-08-14 47 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日編輯。 –

相關問題