gpt4 book ai didi

ios - 无法将类型 'RequestRetrier?' 的值分配给类型 'RequestRetrier?'

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:30 25 4
gpt4 key购买 nike

我正在尝试像 Alamofire 页面中的示例一样重试请求,但出现此错误:

enter image description here

请求重试代码如下:

public typealias RequestRetryCompletion = (_ shouldRetry: Bool, _ timeDelay: TimeInterval) -> Void

public protocol RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion)}

class RequestRetryHandler: RequestRetrier {
public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
completion(true, 10.0) // retry after 1 second

} else {
completion(false, 0.0) // don't retry
}
}}

这就是错误发生的地方:

let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler() as? RequestRetrier

最佳答案

您不应该重新声明协议(protocol)或 RequestRetryCompletion。这已经在 Alamofire 中为您完成了。您只需执行以下操作:

import Alamofire
import Foundation

class RequestRetryHandler: RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
completion(true, 10.0) // retry after 1 second

} else {
completion(false, 0.0) // don't retry
}
}
}

let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler()

祝你好运!

关于ios - 无法将类型 'RequestRetrier?' 的值分配给类型 'RequestRetrier?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47991229/

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