【问题标题】:Alamofire response is allways nilAlamofire 响应始终为零
【发布时间】:2017-01-11 00:52:08
【问题描述】:

我正在尝试使用 Alamofire 4 和 swift 3 编写 REST 客户端的基本示例,但无论我尝试访问什么服务,响应总是为零。

我是否缺少任何配置?

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request("https://httpbin.org/get").responseJSON { response in
        print(response.request)  // original URL request
        print(response.response) // HTTP URL response
        print(response.data)     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
    }
}

播客文件

platform :ios, '9.0'

target 'RESTClient' do

  use_frameworks!

  # Pods for RESTClient
  pod 'Alamofire', '~> 4.2'
  pod 'SwiftyJSON'

end

Info.plist

Info.plist

【问题讨论】:

  • 我觉得有必要在构造函数中包含请求方法类型
  • 在 Alamofire 4 中没有必要。我复制的这段代码与github.com/Alamofire/Alamofire 完全相同

标签: ios swift3 alamofire


【解决方案1】:

只要 Alamofire 使用异步请求,下一部分代码就会在请求之前执行。

因此,为了保证执行顺序,依赖于响应的代码必须放入闭包中。

【讨论】:

  • 你说的clousre是什么意思
猜你喜欢
  • 2021-10-12
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 2023-03-23
  • 2018-08-06
  • 1970-01-01
  • 2023-03-08
  • 2016-05-02
相关资源
最近更新 更多