【问题标题】:Why the Alamofire return early?为什么 Alamofire 会提前回来?
【发布时间】:2016-03-01 05:29:52
【问题描述】:
 Alamofire.request(.GET, gaode, parameters: ["location": location,"key":key])
        .responseJSON { response in
            guard response.response?.statusCode == 200 else{
                return
            }
            js = JSON(response.result.value!)
            guard js["infocode"].string! == "10000" else
            {
                return
            }
            print("JSON: \(js)")
            guard js["regeocode"]["formatted_address"].string != nil else{
                return
            }
            print(js["regeocode"]["formatted_address"].string)
    }
    return js["regeocode"]["formatted_address"].string!

代码在得到响应之前执行return js["regeocode"]["formatted_address"].string!。 为什么?我想在返回之前获取 JSON。 如何解决这个问题?

【问题讨论】:

  • 因为是异步请求。

标签: ios swift alamofire


【解决方案1】:

返回在响应块之外,因此它与您的网络请求完全分开返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2016-09-12
    • 2013-04-18
    • 2015-04-28
    相关资源
    最近更新 更多