将Alamofire.Request扩展从Swift 2迁移到Swift 3

时间:2018-08-09 16:59:09

标签: swift3 swift2 alamofire

我正在尝试将扩展迁移到Alamofire.Request,但遇到错误Cannot call value of non-function type 'HTTPURLResponse?'

我知道编译器认为我指的是成员响应,而不是函数。

我已经用DataRequest.jsonResponseSerializer替换了Request.JSONResponseSerializer。

任何人都能看到我所缺少的吗?

extension Alamofire.Request {
    public func responseSwiftyJSON(_ queue: DispatchQueue? = nil, options: JSONSerialization.ReadingOptions = .allowFragments, completionHandler:  @escaping (NSURLRequest, HTTPURLResponse?, SwiftyJSON.JSON, Error?) -> Void) -> Self {
        return response(responseSerializer: Alamofire.DataRequest.jsonResponseSerializer(options: options)) { response in
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                var responseJSON: JSON

                if response.result.isFailure {
                    responseJSON = JSON.null
                } else {
                    responseJSON = SwiftyJSON.JSON(response.result.value!)
                }

                dispatch_async(queue ?? dispatch_get_main_queue(), {
                    completionHandler(self.request!, self.response, responseJSON, response.result.error)
                })
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定这是否与旧版本兼容,但是我建议您这样重写扩展名:

extension DataRequest {
    public func responseSwiftyJSON(queue: DispatchQueue? = nil, options: JSONSerialization.ReadingOptions = .allowFragments, completionHandler:  @escaping (URLRequest?, HTTPURLResponse?, SwiftyJSON.JSON, Error?) -> Void) -> Self {
        return responseJSON(queue: queue, options: options) {
            let responseJSON: JSON
            switch result {
            case let .success(json): responseJSON = JSON(json)
            case .failure: responseJSON = JSON.null
            }

            completionHandler(request, response, responseJSON, error)
        }
    }
}