Swift 函数返回空数据

标签 swift

我是 Swift 编码的新手,我一直在四处寻找解决这个问题的方法,但至今仍无法找到解决我问题的方法。

我正在使用一个函数从 Firebase 返回一些数据。经过许多错误和大量研究后,我设法使函数“正常工作”而不会引发错误,但返回的数据是空白的。本质上,当我调用该函数时,我试图返回多个值并将它们保存在一个数组中。

数据从 Firebase 返回正常,当我在设置变量后打印出变量时,它会正确打印出来,但是当我在返回函数之前执行相同操作时,它返回空白。如果我试图在设置数据后立即返回该函数,我会收到错误消息“无效函数中意外的非无效返回值”。

完整代码如下:

func loadClientData(client_id: String) -> (address_1: String, address_2: String, city: String, company_name: String, contact_name: String, county: String, email: String, phone: String, postcode: String, country: String) {
        let db = Firestore.firestore()
        let userID : String = (Auth.auth().currentUser!.uid)

        print("\(userID)/clients/existing/\(client_id)")

        let docRef = db.collection("\(userID)/clients/existing/").document(client_id)

        var address_1 = ""
        var address_2 = ""
        var city = ""
        var company_name = ""
        var contact_name = ""
        var county = ""
        var email = ""
        var phone = ""
        var postcode = ""
        var country = ""

        docRef.getDocument { (document, error) in
            if let document = document, document.exists {

                let data = document.data()

                address_1 = data?["address_1"] as? String ?? ""
                address_2 = data?["address_2"] as? String ?? ""
                city = data?["city"] as? String ?? ""
                company_name = data?["company_name"] as? String ?? ""
                contact_name = data?["contact_name"] as? String ?? ""
                county = data?["county"] as? String ?? ""
                email = data?["email"] as? String ?? ""
                phone = data?["phone"] as? String ?? ""
                postcode = data?["postcode"] as? String ?? ""
                country = data?["country"] as? String ?? ""

                print("Company name is \(company_name)") // <---- THIS prints out the company name

            } else {
                print("client does not exist")
                return
            }
        }


        print("Company name is \(company_name)") // <---- THIS prints the company name as blank

        return (address_1: address_1, address_2: address_2, city: city, company_name: company_name, contact_name: contact_name, county: county, email: email, phone: phone, postcode: postcode, country: country)
    }

这是这样调用的:

let companyInfo = loadClientData(client_id: self.items[indexPath.item].company)

        print(companyInfo)

并打印出以下内容:

(address_1: "", address_2: "", city: "", company_name: "", contact_name: "", county: "", email: "", phone: "", postcode: "", country: "")

预先感谢您的输入。

最佳答案

many这个网站上的问题与你的问题基本相同,你应该看看它们。为避免冗余,我将以简单的方式进行解释,但我敦促您阅读类似的问题。

简而言之,发生的事情是 loadClientData 异步 获取数据,这意味着它发生在后台,而其他事情发生在前台。前台内容包括调用 loadClientData 并将其打印出来的代码。 前台不会等待后台完成。

您什么也得不到,因为在您print(companyInfo) 时后台内容尚未处理完毕。

Real world example:

Your mom asks you to go buy a lemon for a dish she is cooking for dinner.

She starts cooking, but she has no lemon!

Why? Because you haven't yet returned from the supermarket, and your mom didn't wait.

如果你想让前台的东西等待,你有很多选择,你可以通过查看other similar questions来考虑。 .

关于Swift 函数返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859855/

相关文章:

swift - 让表格搜索功能正常工作?

ios - 符合协议(protocol)的 UIView 子类的 Swift 数组

swift - 错误 : Could not cast value of type 'NSNull' (0x10e404918) to 'NSString' (0x10d5fac60). (lldb)

ios - 在 Swift 和 iOS 支持中使用空字符串过滤 NSDictionary >= 8.0

ios - 我怎样才能使这个 UIBarButtonItem 变粗?

swift - 使用 PDFKit - iOS 11 点击注释后触发应用内浏览器

string - Swift 中 Int 的前导零

ios - 如何判断 AVPlayer 已播放三秒 Swift

swift - 如何将对象保存到 UserDefaults 或钥匙串(keychain) - Swift 3

swift - 无法将 swift 泛型子类转换为泛型父类(super class)