Swift3 如何将闭包中的变量保存为外部变量?

Swift3 如何将闭包中的变量保存为外部变量?,swift3,closures,alamofire,uipickerview,unbox,Swift3,Closures,Alamofire,Uipickerview,Unbox,我正在尝试创建一个定制的PickerView,它从对web服务器的API调用中获取数据。我遇到的问题是将解析后的数据保存到外部变量中,以便PickerView协议方法可以访问它 // API Call / Parsing using Alamofire + Unbox static func makeApiCall(completionHandler: @escaping (CustomDataStructure) -> ()) { Alamofire.request(webser

我正在尝试创建一个定制的PickerView,它从对web服务器的API调用中获取数据。我遇到的问题是将解析后的数据保存到外部变量中,以便PickerView协议方法可以访问它

// API Call / Parsing using Alamofire + Unbox
static func makeApiCall(completionHandler: @escaping (CustomDataStructure) -> ()) {
    Alamofire.request(webserverUrl, method: .get).responseObject { (response: DataResponse<Experiment>) in
        switch response.result {
            case .success:
                if var configParams = response.result.value {
                    let inputConfigs = removeExtrasParams(experiment: response.result.value!)
                    let modifiedViewModel = modifyViewModel(experiment: &configParams, inputConfigs: inputConfigs)
                    completionHandler(modifiedViewModel)
                }
            case .failure(_):
                break
        }
    }
}

// Custom PickerClass
class CustomPickerView: UIPickerView {
    fileprivate var customDS: CustomDataStructure?

    override init() {
        super.init()

        dataSource = self
        delegate = self

        SomeClass.makeApiCall(completionHandler: { customds in
            self.customDS = customds
        })
    }

    ...
}

extension CustomPickerView: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if let customds = customDS {
            if let customDSValues = customds.inputs.first?.value {
                return customDSValues[row]
            }
        }
        return "apple"
    }
}

extension CustomPickerView: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if let customds = customDS {
            return customds.inputs.values.count
        } else {
            return 0
        }
    }
}
//使用Alamofire+Unbox调用/解析API
静态func makeApiCall(completionHandler:@escaping(CustomDataStructure)->()){
Alamofire.request(webserverUrl,方法:.get).responseObject{(响应:DataResponse)位于
开关响应。结果{
成功案例:
如果var configParams=response.result.value{
让inputConfigs=removeExtrasParams(实验:response.result.value!)
让modifiedViewModel=modifyViewModel(实验:&configParams,inputConfigs:inputConfigs)
completionHandler(modifiedViewModel)
}
案例.失败(uu):
打破
}
}
}
//自定义选择器类
类CustomPickerView:UIPickerView{
fileprivate var customDS:CustomDataStructure?
重写init(){
super.init()
数据源=self
代表=自我
makeApiCall(completionHandler:{customds in
self.customDS=customDS
})
}
...
}
扩展CustomPickerView:UIPickerViewDelegate{
func pickerView(pickerView:UIPickerView,didSelectRow行:Int,不完整组件:Int){
}
func pickerView(pickerView:UIPickerView,titleForRow行:Int,forComponent组件:Int)->String{
如果让customds=customds{
如果让customDSValues=customds.inputs.first?value{
返回customDSValues[行]
}
}
返回“苹果”
}
}
扩展名CustomPickerView:UIPickerViewDataSource{
func numberOfComponents(在pickerView:UIPickerView中)->Int{
返回1
}
func pickerView(pickerView:UIPickerView,numberOfRowsInComponent:Int)->Int{
如果让customds=customds{
返回customds.inputs.values.count
}否则{
返回0
}
}
}
我遇到的问题是customDS每次都返回nil


这里我做错了什么?

makeApiCall
的完成块中,只需在主线程上重新加载
pickerView的
组件,就可以开始了

SomeClass.makeApiCall(completionHandler: { customds in
    self.customDS = customds
    DispatchQueue.main.async {
        self.reloadComponent(0) 
    }
})

makeApiCall
的完成块中,只需在主线程上重新加载
pickerView的
组件,就可以开始了

SomeClass.makeApiCall(completionHandler: { customds in
    self.customDS = customds
    DispatchQueue.main.async {
        self.reloadComponent(0) 
    }
})

您在哪里访问customDS?我添加了访问customDS的代码块。您在哪里访问customDS?我添加了访问customDS的代码块。这成功了,谢谢!出于某种原因,我认为PickerView使用的是reloadData(),就像TableView使用的一样。再次感谢!这就成功了,谢谢!出于某种原因,我认为PickerView使用的是reloadData(),就像TableView使用的一样。再次感谢!