Swift alamofire获取请求返回空值

Swift alamofire获取请求返回空值,swift,alamofire,Swift,Alamofire,我有一个问题。我正在与alamofire重新测试一个it返回值。在alamofire中,我将返回的json转换为我的自定义类,它的工作是goog。但当我在alamofire主体外使用这个转换后的类时,它返回了nil值。 我把断点放在alamofire中,在那个里我转换了json值并出现在我的类中,它有值并把它放在我的类中,但在体外它等于零。为什么? func getZakazDetail(orderID:Int,tableView:UITableView,spinner:UIActivityIn

我有一个问题。我正在与alamofire重新测试一个it返回值。在alamofire中,我将返回的json转换为我的自定义类,它的工作是goog。但当我在alamofire主体外使用这个转换后的类时,它返回了nil值。 我把断点放在alamofire中,在那个里我转换了json值并出现在我的类中,它有值并把它放在我的类中,但在体外它等于零。为什么?

func getZakazDetail(orderID:Int,tableView:UITableView,spinner:UIActivityIndicatorView){

    if flag{
        let zD=ZakazDetailTableViewController()
        Alamofire.request(.GET, "myUrl")
            .responseJSON { response in
                guard response.result.error == nil else {
                    // got an error in getting the data, need to handle it
                    print("error calling GET on /todos/1")
                    print(response.result.error!)
                    return
                }

                if let value = response.result.value {
                    let product = JSON(value)
                    if product != nil{
                        for (_,subJson):(String, JSON) in product {
                            let p=ZakazDetail(id: subJson["id"].int!, tovar: subJson["tovar"].string!, count: subJson["count"].int!, orderid: subJson["orderid"].int!, cena: subJson["cena"].int!)
                            self.zakazDetail.append(p)
                            zD.detail.append(p)
                        }
                    }
                }
        }
        spinner.stopAnimating()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        tableView.reloadData()

    }
    else{
        let alert = UIAlertView(title: "Ошибка",message: "Нету интернета!",delegate: nil,cancelButtonTitle: "OK")
        alert.show()
    }

}

之所以会发生这种情况,是因为alamofire在zD.detail.append(p)中追加任何内容之前异步加载数据。 为程序添加一个完成处理程序,以等待加载完成

//here you call authenticateUser with a closure that prints responseObject
API().authenticateUser{ (responseObject, error) in
    println(responseObject)
}
然后:


有关更多信息,请阅读文档:

之所以发生这种情况,是因为alamofire在zD.detail.append(p)中追加任何内容之前异步加载数据。 为程序添加一个完成处理程序,以等待加载完成

//here you call authenticateUser with a closure that prints responseObject
API().authenticateUser{ (responseObject, error) in
    println(responseObject)
}
然后:


有关更多信息,请阅读文档:

但是已经有了下载json let product=json(value)是的,在alamofire加载数据之前,您应该告诉代码不要附加任何内容,然后一旦完成,就可以附加它并重新加载数据。如下所示:func getZakazDetail(orderID:Int,tableView:UITableView,微调器:UIActivityIndicatorView,completion:(JSON)->())让product=JSON(value)completion(product)Woah Woah这不是添加完成处理程序的方式。请查看我的答案。它应该会对您有所帮助。我已经对它进行了编辑。func getZakazDetailRequest(completionHandler:(responseObject:JSON)->(){makeGetZakazDetail(completionHandler)}但它已经下载了JSON let product=JSON(value)是的,在alamofire加载数据之前,您应该告诉代码不要附加任何内容,然后一旦完成,您就可以附加它并重新加载数据让product=JSON(value)completion(product)Woah Woah这不是添加完成处理程序的方式。请查看我的答案。它应该会对您有所帮助。我已经编辑了它。func getZakazDetailRequest(completionHandler:(responseObject:JSON)->({makeGetZakazDetail)(completionHandler)}