该类不符合协议RequestRetrier

编程入门 行业动态 更新时间:2024-10-14 08:23:48
本文介绍了该类不符合协议RequestRetrier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在将项目迁移到swift3,并一直在努力使Alamofire RequestRetrier协议正常工作。我已遵循Alamofire 4.0迁移指南: github/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#request-retrier

I have been migrating my project to swift3 and have been battling to get Alamofire RequestRetrier protocol to work. I have followed Alamofire 4.0 migrating guide: github/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#request-retrier

这是我要构建的课程:

import Foundation import Alamofire class RequestAccessTokenAdapter: RequestAdapter, RequestRetrier { private let accessToken: String init(accessToken: String) { self.accessToken = accessToken } func adapt(_ urlRequest: URLRequest) throws -> URLRequest { var urlRequest = urlRequest if (urlRequest.url?.absoluteString.hasPrefix(MyServer.serverUrl()))! { urlRequest.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization") } return urlRequest } func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) { if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 { completion(true, 1.0) // retry after 1 second } else { completion(false, 0.0) // don't retry } } }

由于出现以下错误:类型'RequestAccessTokenAdapter'不符合协议'RequestRetrier'

我一直在尝试同时使用Alamofire 4.2.0和AlamofireObjectMapper 4.0.1以及Alamofire 4.0.1& AlamofireObjectMapper 4.0.0,但是我仍然遇到相同的错误。

I have been trying with both Alamofire 4.2.0 & AlamofireObjectMapper 4.0.1 and also with Alamofire 4.0.1 & AlamofireObjectMapper 4.0.0 but I keep getting the same error.

如果我仅使用RequestAdapter协议并删除了should-function,那么一切都会好起来,但是我似乎无法得到要构建的RequestRetrier,这也是我的项目所需要的。

Everything builds ok if I only use RequestAdapter protocol and remove should-function, but I can't seem to get the RequestRetrier to build, which I also need for my project.

知道我班上缺少什么吗?

Any idea what I'm missing from my class?

编辑:

在我用Swift.Error替换了should-function的定义错误后,代码构建成功了,我似乎遇到了一个命名空间问题:

I seemed to have a namespace issue as the code build succeeded after I replaced Error with Swift.Error in the definition of should-function:

func should(_ manager: SessionManager, retry request: Request, with error: Swift.Error, completion: @escaping RequestRetryCompletion) {

推荐答案

我也看到了同样的问题。看了Alamofire源代码后,我发现XCode正在自动为应该方法生成一个无效的方法签名。通过将 Alamofire 模块名称显式添加到 SessionManager ,请求和 RequestRetryCompletion 类型声明,在应该方法的参数列表中,我终于能够构建它。因此,您的应该方法应如下所示:

I too was seeing the same issue. After having a look at the Alamofire source code, I found that XCode is auto-generating an invalid method signature for the should method. By explicitly adding the Alamofire module name to the SessionManager, Request and RequestRetryCompletion type declarations, in the should method's argument list, I was finally able to get this to build. So, your should method should look something like this:

func should(_ manager: Alamofire.SessionManager, retry request: Alamofire.Request, with error: Error, completion: @escaping Alamofire.RequestRetryCompletion) { // Do something }

我希望这会有所帮助!

更多推荐

该类不符合协议RequestRetrier

本文发布于:2023-11-26 08:43:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633419.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不符合   协议   RequestRetrier

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!