我无法在任何地方访问我的数组对象,如何在快速中访问它们?

如何在另一个函数中访问数组对象,即ARR和电子邮件中的数组对象。到目前为止,我只能在调用auth.auth()函数时访问Else语句中的数组对象。我想知道如何才能做到这一点。

let store = CNContactStore()
            
            store.requestAccess(for: .contacts) { (granted, err) in
                if let err = err{
                    print(err.localizedDescription)
                    return
                }
                if granted{
                    print("Access granted")
                    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey]
                    let req = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
                    do {
                        
                        try store.enumerateContacts(with: req) { (contact, stop) in
                            print(contact.emailAddresses.first?.value as Any)
                            if let em = contact.emailAddresses.first?.value{
                                //print("cool")
                                Auth.auth().fetchProviders(forEmail: em as String, completion: {
                                    (providers, error) in
    
                                    if error != nil {
                                        print("wierd")
                                    }else{
                                        if providers == nil{
                                            print("No active account")
                                        }else{
                                            self.emails.append(em as String)
                                            self.arr.append(contact.givenName + " " + contact.familyName)
                                            print("Active Account")
                                            print(self.arr)
                                            print(self.emails)
                                        }
                                    }
                                })
                            
                            }else{
                                //print("wow")
                            }
                        }
                        
                    }catch let err{
                        print(err.localizedDescription)
                    }
                }else{
                    print("Access denied")
                }
            }
            print("Arr")
            print(self.arr)
            print(self.emails)
            print("Emails")

我想知道如何访问另一个函数中的数组元素和电子邮件中的数组元素,因为我的最终目标是将数组信息发布到一个TableView中。


解决方案

数据从Firebase异步加载。虽然您可以将其存储在可从任何位置访问的变量中,但时间很重要。

在您的代码中,当print(self.arr)运行时,self.arr.append(...)尚未运行。通过在这些行上放置断点并在调试器中运行代码,或者通过放置一些日志记录代码并检查其输出,您可以最容易地看到这一点。

因此,需要数据库中数据的所有代码都需要在完成处理程序中或从那里调用。

有关此问题的更多信息和解决方案示例,请参阅:

  • Code not finishing the function, it is ending execution halfway through,包含更多链接
  • Make code with Firebase asynchronous
  • Why isn't my function that pulls information from the database working?,链接更多
  • Finish all asynchronous requests before loading data?,使用调度组
  • Asynchronous Swift call into array
  • using variables outside of completion block
  • wait for two asynchronous completion functions to finish, before executing next line code
  • Finish all asynchronous requests before loading data?

相关文章

Swift数组

2023-07-19 swift 数组