ResponseSerializer 'cannot call a value of non-function type "NSHTTPURLResponse?" With Swift 3

I used the following code without problems before upgrading to Xcode 8 beta 6. It is similar to this example from the Alamofire repository. This morning I updated my Alamofire library to the last branch swift3, which is now compatible with beta 6. It shows an 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.

From what I understand, this error occurs due to the fact that she thinks I'm trying to return a property Request responseinstead of a function response(responseSerializer: <T>, completionHandler: <(Response<T.SerializedObject, T.ErrorObject>) -> Void>), and she thinks about it because of a type error in responseSerializeror completionHandlerthat I pass to the function.

How can I configure this code to be compatible with function declaration and compiler?

I added Handler @escapingto the end to fix 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?'
}
}
+4
source share
2 answers

You need to note completionHandlerhow @escaping.

+8
source

I still saw this error even after adding @escapingto the closure. The problem was that I needed to change the extension declaration from extension Alamofire.Request { }to extension Alamofire.DataRequest { }.

+3
source

Source: https://habr.com/ru/post/1651590/


All Articles