Swift 如何在异步函数中更改变量值?

Swift 如何在异步函数中更改变量值?,swift,google-cloud-firestore,Swift,Google Cloud Firestore,我正在尝试将数组的值设置为firebase firestore中存储的两个对象。然后我尝试将数组设置为UIPicker。请告诉我我的func声明有什么问题: func getDropDownOptions(completion: @escaping (_ in1: String,_ in2: String,_ in3: String,_ in4: String,_ in5: String) -> Void) 或者我的使命: getDropDownOptions { (in1, in2,

我正在尝试将数组的值设置为firebase firestore中存储的两个对象。然后我尝试将数组设置为UIPicker。请告诉我我的func声明有什么问题:

func getDropDownOptions(completion: @escaping (_ in1: String,_ in2: String,_ in3: String,_ in4: String,_ in5: String) -> Void)
或者我的使命:

getDropDownOptions { (in1, in2, in3, in4, in5) in
            if in1 == "nil" {
                self.errorLabel.text = "Please add an intrest to your profile"
                return
            } else if in2 == "nil" {

                return self.sportPickerData = [in1, in2]
            } else if in3 == "nil" {

                return self.sportPickerData = [in1, in2, in3]
            } else if in4 == "nil" {
                return self.sportPickerData = [in1, in2, in3, in4]
            } else {
                return self.sportPickerData = [in1, in2, in3, in4, in5]
            }
        }

如果在后台线程上调用完成,则需要将其分派回主线程以更改UI

// if you are in a background thread, then ...
DispatchQueue.main.async {
    // Update UI here
}
因此,对于错误标签,您肯定应该这样做。对于其他变量,您可以设置它们,但是如果它们导致UI更改,您可能应该在主线程中进行设置

例如:

// if you are in a background thread, then ...
DispatchQueue.main.async {
    // Update UI here
    self.errorLabel.text = "Please add an intrest to your profile"
}

它做错了什么?它应该做什么?您不应该试图
返回赋值运算符的结果,请尝试删除这些运算符,看看是否有帮助。只需返回所需的数组即可。返回赋值将返回
void
,而不是数组。我输入了示例代码——但是,这可能无法解决您的问题——这是一个问题,但可能不是唯一的问题。你还没有告诉我们发生了什么,这是错误的