简体   繁体   English

ResponseSerializer'无法使用Swift 3调用非函数类型'NSHTTPURLResponse?'的值

[英]ResponseSerializer 'cannot call value of non-function type 'NSHTTPURLResponse?'' with Swift 3

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. 在更新到Xcode 8 beta 6之前,我一直在使用以下代码而没有问题。它与Alamofire存储库中的此示例类似。 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?' 今天早上我将我的Alamofire库更新到最新的swift3分支,现在与beta 6兼容。它显示错误: 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. 这里存在类似的问题,但它不是基于当前版本的Swift和Alamofire。

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. 据我所知,这个错误是因为它认为我试图返回Request属性response而不是函数response(responseSerializer: <T>, completionHandler: <(Response<T.SerializedObject, T.ErrorObject>) -> Void>)并且它认为这是因为我传递给函数的responseSerializercompletionHandler中的类型错误。

How can I adjust this code to make it compatible with the function declaration and compiler? 如何调整此代码以使其与函数声明和编译器兼容?

I added @escaping to the completionHandler to correct the error. 我将@escaping添加到completionHandler以更正错误。

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标记为@escaping

I was still seeing this error even after adding @escaping to the closure. 即使将@escaping添加到闭包中,我仍然看到此错误。 The issue I had was that I needed to change my extension declaration from extension Alamofire.Request { } to extension Alamofire.DataRequest { } . 我遇到的问题是我需要将扩展​​声明从extension Alamofire.Request { }更改为extension Alamofire.DataRequest { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift 3中无法调用非函数类型&#39;UICollectionView&#39;的值 - Cannot Call Value of Non-Function Type 'UICollectionView' in Swift 3 无法调用非函数类型的值 &#39;((AnyObject) -&gt; AnyObject?)!&#39; - 斯威夫特 3 - Cannot call value of non-function type '((AnyObject) -> AnyObject?)!' - Swift 3 错误:Swift 3上的“无法调用非函数类型的值” - Error: 'Cannot call value of non-function type' on Swift 3 快速错误:无法调用非函数类型“ SKSpriteNode”的值 - Swift error: Cannot call value of non-function type “SKSpriteNode” 无法调用非功能类型的值 - Cannot call value of non-function type 无法调用非功能类型的值 - Cannot call value of non-function type Swift:类型的值没有成员,无法调用非函数类型的值 - Swift: value of type has no member, cannot call value of non-function type 无法在 Swift 中的 function 错误中调用非函数类型 'SwipeCard' 的值 - Cannot call value of non-function type 'SwipeCard' in the function error in Swift 无法调用非函数类型&#39;JSON&#39;的值,如何从Swift中的块返回值 - Cannot call value of non-function type 'JSON', How can I return value from block in Swift 无法调用非功能类型“ Any ?!”的值 :-Firebase,Swift3 - Cannot call value of non-function type 'Any?!' :- Firebase,Swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM