ios 如何在 swift 2 中获取 Alamofire.request().responseJSON 的结果值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32018741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the result value of Alamofire.request().responseJSON in swift 2?
提问by Edgar Georgel
I have a question about the new version of Alamofire for Swift 2
我有一个关于新版 Alamofire for Swift 2 的问题
Alamofire.request(.POST, urlString, parameters: parameters as? [String : AnyObject])
.responseJSON { (request, response, result) -> Void in
let dico = result as? NSDictionary
for (index, value) in dico! {
print("index : \(index) value : \(value)")
}
}
In this section I would like to cast the result in to a NSDictionary. But When I compile and put a breakpoint, the debugger says that dico is nil. If I use debugDescription to print result, it is not nil and contains what I expected How can I cast the Result variable?
在本节中,我想将结果转换为 NSDictionary。但是当我编译并放置断点时,调试器说 dico 为零。如果我使用 debugDescription 打印结果,它不是 nil 并且包含我期望的内容 如何转换 Result 变量?
回答by Javier Cadiz
The accepted answer works great but with the introduction of Alamofire 3.0.0 there are some breaking changes that affects this implementation.
The migration guidehas further explanations but i will highlight the ones related to the actual solution.
接受的答案效果很好,但随着 Alamofire 3.0.0 的引入,有一些影响此实现的重大更改。
该迁移指南有进一步的解释,但我会强调与实际的解决方案的人。
Response
All response serializers (with the exception of response) return a generic Response struct.Response type
The Result type has been redesigned to be a double generic type that does not store the NSData? in the.Failure
case.
Also take in count that Alamofire treats any completed request to be successful, regardless of the content of the response. So you need to chain a .validate()
before .responseJSON()
to hit the .Failure
case.
Read more about it here.
还要考虑到 Alamofire 将任何已完成的请求视为成功,而不管响应的内容如何。所以你需要先链接 a.validate()
才能.responseJSON()
命中.Failure
案例。在此处阅读更多相关信息。
Updated code:
更新代码:
let url = "http://api.myawesomeapp.com"
Alamofire.request(.GET, url).validate().responseJSON { response in
switch response.result {
case .Success(let data):
let json = JSON(data)
let name = json["name"].stringValue
print(name)
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
For reference:
以供参考:
- Xcode 7.3 (Swift 2.2)
- Alamofire 3.3.1
- SwiftyJSON 2.3.3
- Xcode 7.3 (Swift 2.2)
- 阿拉莫火 3.3.1
- SwiftyJSON 2.3.3
回答by chemic
If you don't mind using SwiftyJSONlibrary, here's a working example in Xcode 7 Beta 5 + Alamofire 2.0.0-beta.1 + SwiftyJSON (xcode7 branch)
如果您不介意使用SwiftyJSON库,这里有一个在 Xcode 7 Beta 5 + Alamofire 2.0.0-beta.1 + SwiftyJSON(xcode7 分支)中的工作示例
Alamofire.request(.GET, url, parameters: params, encoding: ParameterEncoding.URL).responseJSON { (_, _, result) in
switch result {
case .Success(let data):
let json = JSON(data)
let name = json["name"].string
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
}
Edit:
编辑:
Updated SwiftyJSONgit page
更新了SwiftyJSONgit 页面