该类不符合协议RequestRetrier [英] Class does not conform to protocol RequestRetrier

查看:261
本文介绍了该类不符合协议RequestRetrier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将项目迁移到swift3,并一直在努力使Alamofire RequestRetrier协议正常工作。我已遵循Alamofire 4.0迁移指南:
https://github.com/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: https://github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆