Alamofire目前是版本3.3,根据存储库中的发布情况,自版本3.0以来已有一些更改。
如果您使用
响应处理程序,则闭包应如下所示:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.response { request, response, data, error in
print(request)
print(response)
print(data)
print(error)
}
如果你使用例如 响应JSON处理程序,现在所有内容都封装在 response
中,就像这段代码一样:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request)
print(response.response)
print(response.data)
print(response.result)
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
或者,您可以使用以下代码更轻松地处理:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
switch(response.result) {
case .Success(let value):
if let JSON = value {
print("JSON: \(JSON)")
}
case .Failure(let error):
print(error.description)
}
}
我希望这能帮到你。