本文介绍了无法调用非功能类型“ NSHTTPURLResponse?”的值Alamofire ObjectMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法调用非功能类型'NSHTTPURLResponse'的值?'

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

有人可以在这里帮助我吗?

Can someone please help me here?

这是代码

   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)
            }
        }
    }
}

我不好,我没有注意到Alamofire 2.0的返回类型,

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)
            }
        }

    }
}


推荐答案

我自己曾遇到此错误,花了一段时间才弄清楚为什么会发生。看起来如果 response()方法调用的参数与任何方法声明都不匹配,则swift假定您的代码引用了 response 属性,即 NSHTTPURLResponse 。因为存在遮盖方法名称的属性,所以swift不能通过指出哪个参数是问题的错误来帮助您,因此它会在与任何方法匹配时进行调整。

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.

在我的案例中,由于 参数, completionHandler 不匹配。请注意,我看到的示例代码 .response {response in ...} 是有问题的。没有像 responseString 那样的 response 方法采用 response in关闭responseJSON 等方法。

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.

这篇关于无法调用非功能类型“ NSHTTPURLResponse?”的值Alamofire ObjectMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:11