2017-08-14 12 views
-4

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) 
      } 
     } 
    } 
+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日の編集であなたのインデントを修正しました。 –

関連する問題