无法分配“RequestRetrier”类型的值?输入'RequestRetrier?'

时间:2017-12-27 11:18:12

标签: ios swift networking alamofire

我试图让请求更像Alamofire页面中的示例,但我收到此错误:

enter image description here

这是Request Retrier代码:

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

1 个答案:

答案 0 :(得分:4)

您不应该重新声明协议或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()

祝你好运!