swift2 正在迁移Alamofire,请求从Swift 2扩展到Swift 3

kq4fsx7k  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(237)

我试图迁移一个扩展到Alamofire。请求,但得到错误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)
                })
            }
        }
    }
}
siv3szwd

siv3szwd1#

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

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)
        }
    }
}

相关问题