Mid*_*yan 1 ios alamofire codable swift5
对于 codable 新手,我尝试使用 codable 为 alamofire 创建一个类,并尝试发出 api 请求。我收到一些 Swift.DecodingError.typeMismatch 错误,并且由于我的模型类而发现了它。现在我需要的是在解码之前以 JSON(String) 格式打印 alamofire 响应,以便我可以识别 typeMismatch
static func performRequest<T:Decodable>(route:APIRouter, decoder: JSONDecoder = JSONDecoder(), completion:@escaping (Result<T,Error>)->Void) -> DataRequest {
return AF.request(route)
.responseDecodable (decoder: decoder){ (response: DataResponse<T>) in
print(response.result)
completion(response.result)
}
}
Run Code Online (Sandbox Code Playgroud)
我想要一些代码来打印 alamofire 的实际结果
您可以通过从以下位置获取来打印闭包Data中的原始数据:responseDecodableDataResponse
print(response.data.map { String(decoding: $0, as: UTF8.self) } ?? "No data.")
Run Code Online (Sandbox Code Playgroud)
您还可以添加一个单独的序列化器来查看String:
.responseDecodable { }
.responseString { }
Run Code Online (Sandbox Code Playgroud)
如果您只想查看响应以进行调试,则可以debugPrint在闭包中查看响应。这会将请求和响应正文数据打印为Strings。
print(response.data.map { String(decoding: $0, as: UTF8.self) } ?? "No data.")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1884 次 |
| 最近记录: |