ResponseSerializer 'cannot call value of non-function type 'NSHTTPURLResponse?'' with Swift 3
Asked Answered
C

2

5

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.

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?

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?'
}
}
Carleycarli answered 17/8, 2016 at 18:28 Comment(5)
Could you rename the parameter response in your closure to something else?Cabdriver
BTW, I suspect that SE-0103 is what caused the problem for you in this case.Cabdriver
@J.Cocoe Thanks, but I just now updated the closure to ResponseSerializer<T, NSError> { request, res, data, error in and the error persists.Carleycarli
Yeah, it's picking the outside one. Try changing the return response(...) to return res(...), too?Cabdriver
@J.Cocoe Turns out that adding @escaping was the solution.Carleycarli
C
8

You need to mark your completionHandler as @escaping.

Clevis answered 17/8, 2016 at 19:13 Comment(1)
Thanks! That did it.Carleycarli
P
3

I was still seeing this error even after adding @escaping to the closure. The issue I had was that I needed to change my extension declaration from extension Alamofire.Request { } to extension Alamofire.DataRequest { }.

Pleiad answered 7/10, 2016 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.