ResponseSerializer'无法使用Swift 3调用非函数类型'NSHTTPURLResponse?''的值 [英] ResponseSerializer 'cannot call value of non-function type 'NSHTTPURLResponse?'' with Swift 3

查看:269
本文介绍了ResponseSerializer'无法使用Swift 3调用非函数类型'NSHTTPURLResponse?''的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新到Xcode 8 beta 6之前,我一直在使用以下代码。它类似于来自Alamofire存储库的此示例。今天早上我将我的Alamofire库更新到最新的 swift3 分支,现在与beta 6兼容。它显示错误:无法调用非值-function type'HTTPURLResponse?'存在类似的问题这里,但它不是基于当前版本的Swift和Alamofire。

I had been using the following code without issue until updating to Xcode 8 beta 6. It is similar to this example from the Alamofire repository. This morning I updated my Alamofire library to the latest swift3 branch, which is now compatible with beta 6. It shows the error: Cannot call value of non-function type 'HTTPURLResponse?' A similar question exists here, but it is not based on the current version of Swift and Alamofire.

据我所知,这个错误是因为它认为我我试图返回请求属性响应而不是函数响应(responseSerializer:< T>,completionHandler:<(响应< T.SerializedObject,T.ErrorObject>) - > Void>)并且它认为这是因为中的类型错误我正在传递给函数的responseSerializer completionHandler

From what I understand, this error is because it thinks that I am trying to return the Request property response instead of the function response(responseSerializer: <T>, completionHandler: <(Response<T.SerializedObject, T.ErrorObject>) -> Void>) and it thinks this because of a type error in either the responseSerializer or completionHandler that I'm passing into the function.

如何调整此代码以使其与函数声明和编译器兼容?

How can I adjust this code to make it compatible with the function declaration and compiler?

我添加 @escaping 到completionHandler来纠正错误。

I added @escaping to the completionHandler to correct the error.

import Foundation
import Alamofire
import SwiftyJSON

extension Alamofire.Request {
public func responseObject<T: ResponseJSONObjectSerializable>(_ completionHandler: @escaping (Response<T, NSError>) -> Void) -> Self {
    let responseSerializer = ResponseSerializer<T, NSError> { request, res, data, error in

        guard let responseData = data else {
            let error = DFError.error(withDFCode: .dataSerializationFailed, failureReason: "Data could not be serialized because input data was nil.")
            return .failure(error)
        }

        let jsonData: Any?
        do {
            jsonData = try JSONSerialization.jsonObject(with: responseData, options: [])
        } catch  {
            let error = DFError.error(withDFCode: .jsonSerializationFailed, failureReason: "JSON could not be serialized into response object")
            return .failure(error)
        }

        let json = SwiftyJSON.JSON(jsonData!)
        if let newObject = T(json: json) {

            return .success(newObject)
        }

        let error = DFError.error(withDFCode: .jsonSerializationFailed, failureReason: "JSON could not be serialized into response object")
        return .failure(error)
    }

    return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
    //Error: Cannot call value of non-function type 'HTTPURLResponse?'
}
}


推荐答案

你需要标记你的 completionHandler as @escaping

You need to mark your completionHandler as @escaping.

这篇关于ResponseSerializer'无法使用Swift 3调用非函数类型'NSHTTPURLResponse?''的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆