gpt4 book ai didi

swift3 - 类不符合协议(protocol) RequestRetrier

转载 作者:行者123 更新时间:2023-12-04 12:18:54 27 4
gpt4 key购买 nike

我一直在将我的项目迁移到 swift3,并且一直在努力让 Alamofire RequestRetrier 协议(protocol)发挥作用。我遵循了 Alamofire 4.0 迁移指南: 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”不符合协议(protocol)“RequestRetrier”

我一直在尝试使用 Alamofire 4.2.0 和 AlamofireObjectMapper 4.0.1 以及 Alamofire 4.0.1 和 AlamofireObjectMapper 4.0.0,但我总是遇到同样的错误。

如果我只使用 RequestAdapter 协议(protocol)并删除 should-function,一切都可以构建,但我似乎无法构建 RequestRetrier,而我的项目也需要它。

知道我在类里面遗漏了什么吗?

编辑:

我似乎遇到了命名空间问题,因为在我将 Error 替换为 Swift.Error 后,代码构建成功了。在 should-function 的定义中:

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

最佳答案

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

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

// Do something

}

希望对您有所帮助!

关于swift3 - 类不符合协议(protocol) RequestRetrier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926681/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com