如何使用Alamofire 6使用响应响应的响应响应6

发布于 2025-01-28 03:23:51 字数 933 浏览 4 评论 0原文

我的一个应用程序之一不再起作用,因为使用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 技术交流群。

扫码二维码加入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 by ResponseType(.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.

两相知 2025-02-04 03:23:51

在Alamofire->文档/usage.md,我找到了可解码的定义,所以我尝试这样,然后找到工作。

struct DecodableType: Decodable { 
    let url: String 
}

AF.request(urlString).responseDecodable(of: DecodableType.self) { response in
    switch response.result {
        case .success(let dTypes):
            print(dTypes.url)
        case .failure(let error):
            print(error)
    }
}

In Alamofire -> Documentation/Usage.md, I found the definition of the Decodable, so I try like this, then found working.

struct DecodableType: Decodable { 
    let url: String 
}

AF.request(urlString).responseDecodable(of: DecodableType.self) { response in
    switch response.result {
        case .success(let dTypes):
            print(dTypes.url)
        case .failure(let error):
            print(error)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文