swift2中如何获取Alamofire.request().responseJSON的结果值?
How to get the result value of Alamofire.request().responseJSON in swift 2?
我对 Swift 2
的新版 Alamofire 有疑问
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)")
}
}
在本节中,我想将结果转换为 NSDictionary。但是当我编译并放置断点时,调试器说 dico 是 nil。如果我使用 debugDescription 打印结果,它不是 nil 并且包含我所期望的
如何转换结果变量?
如果您不介意使用 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)")
}
}
编辑:
已更新 SwiftyJSON git 页面
已接受的答案效果很好,但随着 Alamofire 3.0.0 的引入,有一些影响此实现的重大更改。
migration guide 有进一步的解释,但我将突出显示与实际解决方案相关的解释。
Response
所有响应序列化程序(响应除外)return 通用响应结构。
Response type
Result 类型是否已重新设计为不存储 NSData 的双通用类型?在 .Failure
的情况下。
还要考虑到 Alamofire 将任何已完成的请求视为成功,而不管响应的内容如何。所以你需要在 .responseJSON()
之前链接一个 .validate()
来命中 .Failure
的情况。
阅读更多相关信息 here。
更新代码:
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)")
}
}
供参考:
- Xcode 7.3 (Swift 2.2)
- Alamofire 3.3.1
- SwiftyJSON 2.3.3
例如,您现在无需 SwiftyJSON 即可立即实现大部分所需的行为。我的 OAuthTokenResponse 是一个 Codable 的简单结构。 Alamofire 库 5.2.2 允许您使用 'responseDecodable'
响应
如果你说了一个看起来像这样的结构:
struct OAuthTokenResponse : Codable
{
var access_token:String?
var token_type:String?
var expires_in:Int?
var scope:String?
}
然后在你的网络调用中(使用 Alamofire)
let request = AF.request(identityUrl, method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
request
.validate()
.responseDecodable { (response:AFDataResponse<OAuthTokenResponse>) in
switch response.result {
case .success(let data):
do {
let jwt = try decode(jwt: data.access_token!) // example
self.connected = true
print(jwt)
} catch {
print(error.localizedDescription)
self.connected = false
}
case .failure(let error):
self.connected = false
print(error.localizedDescription)
}
}
在上面的代码中,成功案例会使用可解码协议自动将您的 JSON 反序列化到您的结构中。任何错误都将导致错误案例被命中。
我对 Swift 2
的新版 Alamofire 有疑问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)")
}
}
在本节中,我想将结果转换为 NSDictionary。但是当我编译并放置断点时,调试器说 dico 是 nil。如果我使用 debugDescription 打印结果,它不是 nil 并且包含我所期望的 如何转换结果变量?
如果您不介意使用 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)")
}
}
编辑:
已更新 SwiftyJSON git 页面
已接受的答案效果很好,但随着 Alamofire 3.0.0 的引入,有一些影响此实现的重大更改。
migration guide 有进一步的解释,但我将突出显示与实际解决方案相关的解释。
Response
所有响应序列化程序(响应除外)return 通用响应结构。Response type
Result 类型是否已重新设计为不存储 NSData 的双通用类型?在.Failure
的情况下。
还要考虑到 Alamofire 将任何已完成的请求视为成功,而不管响应的内容如何。所以你需要在 .responseJSON()
之前链接一个 .validate()
来命中 .Failure
的情况。
阅读更多相关信息 here。
更新代码:
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)")
}
}
供参考:
- Xcode 7.3 (Swift 2.2)
- Alamofire 3.3.1
- SwiftyJSON 2.3.3
例如,您现在无需 SwiftyJSON 即可立即实现大部分所需的行为。我的 OAuthTokenResponse 是一个 Codable 的简单结构。 Alamofire 库 5.2.2 允许您使用 'responseDecodable'
响应如果你说了一个看起来像这样的结构:
struct OAuthTokenResponse : Codable
{
var access_token:String?
var token_type:String?
var expires_in:Int?
var scope:String?
}
然后在你的网络调用中(使用 Alamofire)
let request = AF.request(identityUrl, method: .post, parameters: parameters, encoding: URLEncoding.httpBody)
request
.validate()
.responseDecodable { (response:AFDataResponse<OAuthTokenResponse>) in
switch response.result {
case .success(let data):
do {
let jwt = try decode(jwt: data.access_token!) // example
self.connected = true
print(jwt)
} catch {
print(error.localizedDescription)
self.connected = false
}
case .failure(let error):
self.connected = false
print(error.localizedDescription)
}
}
在上面的代码中,成功案例会使用可解码协议自动将您的 JSON 反序列化到您的结构中。任何错误都将导致错误案例被命中。