- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在将我的项目迁移到 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
模块名称显式添加到 SessionManager
、Request
和 RequestRetryCompletion
类型声明中,在 应该
方法的参数列表,我终于能够构建它。因此,您的 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/
我正在尝试像 Alamofire 页面中的示例一样重试请求,但出现此错误: 请求重试代码如下: public typealias RequestRetryCompletion = (_ shouldR
我一直在将我的项目迁移到 swift3,并且一直在努力让 Alamofire RequestRetrier 协议(protocol)发挥作用。我遵循了 Alamofire 4.0 迁移指南: http
我正在使用 RequestRetrier 自动续订 API 的 access_token。但在每个请求函数中,我想捕获超时错误,但是 .responseJSON 正文中的 .case(let erro
我正在尝试实现一种重试机制,我看到 alamofire 有一个。我正在尝试实现一个简单的重试机制,并指定请求的次数,但出现了问题。 class OAuth2Handler: RequestAdapte
我正在使用 Alamofire 实现 RequestRetrier 以刷新给定用户的 accessToken。 func should(_ manager: SessionManager, retry
我正在使用 Alamofire 4.0 的 RequestRetrier 来控制对过期访问 token 的请求的重试。我正在关注文档 here . 我有一个与文档 OAuth2Handler 中提供的
我是一名优秀的程序员,十分优秀!