Alamofire 打印请求给出 nil
Posted
技术标签:
【中文标题】Alamofire 打印请求给出 nil【英文标题】:Alamofire print request gives nil 【发布时间】:2018-04-26 21:57:35 【问题描述】:您好,我尝试从请求中打印,但结果为零
Alamofire.request("http://.../api/v1.8/set/order/", method: .get, parameters: params)
.responseString response in
switch response.result
case .success(let value):
print("value\(value)")
var json = JSON(value)
let nass = json["description"].string
print("nass\(nass)")
in cinsole 打印我
value "result":"code":"15","description":"Invalid account email"
nass nil
【问题讨论】:
【参考方案1】:Alamofire.request("http://xincal.ru/api/v1.8/set/order/", method: .get, parameters: params).responseJSON response in
if let r = response.result.value as? NSDictionary
if let result = r?.value(forKey: "result") as? NSDictionary
if let nass = result.value(forKey: "description") as? String
print(nass)
else
print("Connection error")
但我们有更好的方法来使用 Codable 解析响应数据。
为响应准备模型
struct Result: Codable
let code: String
let description: String
struct Response: Codable
let result: Result
Alamofire.request("http://xincal.ru/api/v1.8/set/order/", method: .get, parameters: params).responseJSON response in
if let data = response.data
let object = try! JSONDecoder().decode(Response.self, from: jsonData)
print(object.result.description)
else
print("Connection error")
【讨论】:
以上是关于Alamofire 打印请求给出 nil的主要内容,如果未能解决你的问题,请参考以下文章
swift 3 alamofire - 获取请求给出响应序列化失败
iOS Swift 2 - 失败时的 Alamofire 打印响应值