如何使用Alamofire 6使用响应响应的响应响应6
我的一个应用程序之一不再起作用,因为使用Alamofire时JSON序列化失败。
'Responsejson(队列:datapreprocessor:emptyResponsecodes:emptyRequestMethods:options:postionHandler :)':)' 被弃用:响应折扣,将被删除 Alamofire 6。使用可响应的可统计。
对于使用以下行
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in.. }
进行更改时
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable { response in... }
的代码,我会收到错误
generic参数't'无法推断
因此我添加以下
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable(of: ResponseType.self) { response in.. }
我会收到错误
在范围中找不到'ResponseType'
有人有任何建议吗?
One of my apps no longer works due to JSON serialisation failing when using Alamofire.
'responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)'
is deprecated: responseJSON deprecated and will be removed in
Alamofire 6. Use responseDecodable instead.
For code with the following lines
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in.. }
When changing to
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable { response in... }
Then I get the error
Generic parameter 'T' could not be inferred
So I add the following
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:])
.responseDecodable(of: ResponseType.self) { response in.. }
I get the error
Cannot find 'ResponseType' in scope
Does anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与
不同。Responsejson
返回字典或数组.ResponsEdecodable
对json的质量序列化为结构或类。您必须创建一个适当的模型,该模型符合
可解码的
,在您的代码中,它以ponsponseType(.sers)为代表
。成功
情况的关联值是模型的根结构。但是弃用(在黄色警告中)意味着API仍在运行。
旁注:JSON词典是从不
[字符串:wone?]
值始终是非偏见的。Unlike
.responseJSON
which returns a dictionary or array.responseDecodable
deserializes the JSON into structs or classes.You have to create an appropriate model which conforms to
Decodable
, in your code it's represented byResponseType(.self)
.The associated value of the
success
case is the root struct of the model.But deprecated (in a yellow warning) means the API is still operational.
Side note: A JSON dictionary is never
[String: Any?]
the value is always non-optional.在Alamofire->文档/usage.md,我找到了可解码的定义,所以我尝试这样,然后找到工作。
In Alamofire -> Documentation/Usage.md, I found the definition of the Decodable, so I try like this, then found working.