无法调用非功能类型“ NSHTTPURLResponse?”的值Alamofire ObjectMapper

编程入门 行业动态 更新时间:2024-10-28 14:28:19
本文介绍了无法调用非功能类型“ 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.

也就是说, Abh ,从看您的代码到底出了什么问题,我看不出来。

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

更多推荐

无法调用非功能类型“ NSHTTPURLResponse?”的值Alamofire ObjectMapper

本文发布于:2023-11-26 05:52:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   功能   NSHTTPURLResponse   Alamofire   ObjectMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!