Cannot call value of non-function type 'NSHTTPURLResponse?' Alamofire ObjectMapper

北城以北 提交于 2019-12-04 03:13:38

问题


Cannot call value of non-function type 'NSHTTPURLResponse?'

Can someone please help me here?

Here is the code

   public func responseObject<T: Mappable>(queue: dispatch_queue_t?, keyPath: String?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, ErrorType?) -> Void) -> Self {

    return response(queue: queue, responseSerializer: Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)) { request, response, result in
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            let parsedObject = Mapper<T>().map(keyPath != nil ? result.value?[keyPath!] : result.value)

            dispatch_async(queue ?? dispatch_get_main_queue()) {
                completionHandler(self.request!, self.response, parsedObject, result.value ?? result.data, result.error)
            }
        }
    }
}

My bad, I did not notice the return type of Alamofire 2.0,

This is fixed, updated code is here

public func responseObject<T: Mappable>(queue: dispatch_queue_t?, keyPath: String?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, ErrorType?) -> Void) -> Self {

let serializer = Request.JSONResponseSerializer(options: NSJSONReadingOptions.AllowFragments)

return response(queue: queue, responseSerializer: serializer) { (Response) -> Void in

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            let parsedObject = Mapper<T>().map(keyPath != nil ? Response.result.value?[keyPath!] : Response.result.value)



            dispatch_async(queue ?? dispatch_get_main_queue()) {
                completionHandler(self.request!, self.response, parsedObject, Response.result.value ?? Response.result.value, Response.result.error)
            }
        }

    }
}

回答1:


Had this error myself and took a while to figure out why it was occurring. It looks like if the parameters to the response() method call don't match to any method declarations then swift assumes your code is referring to the response property, a NSHTTPURLResponse. Because there the property that "shadows" the method's name, swift can't help you out with errors that indicate which parameter is a problem, it just punts on matching to any method.

In my case, completionHandler was mismatched because of its parameters. Note, its the sample code I saw .response { response in ... } that's problematic. There's no response method taking a "response in" closure like there is for the responseString, responseJSON, etc. methods.

That said, Abh, I can't tell from looking what the exact issue is with your code.




回答2:


You need to mark your completionHandler as @escaping.



来源:https://stackoverflow.com/questions/32932748/cannot-call-value-of-non-function-type-nshttpurlresponse-alamofire-objectmapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!