Cannot convert value of type "T?" to the expected type of arguments '_?' - Common types and completion blocks

I am trying to implement AlamofireObjectMapper ( https://github.com/tristanhimmelman/AlamofireObjectMapper ) using Alamofire 3 and the latest version of ObjectMapper ( https://github.com/Hearst-DD/ObjectMapper ).

It seems that AlamofireObjectMapper not been updated to work with Alamofire 3, so I'm trying to do it myself.

I came to this piece of code and now I'm stuck.

It seems that the generic type T is not available inside the response completion block. Is there a change in Alamofire 3 or a change in Swift 2.1?

This is mistake:

Cannot convert value of type 'T?' to the expected type of arguments '_?'

  public func responseObject<T: Mappable>(queue: dispatch_queue_t?, keyPath: String?, completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, AnyObject?, ErrorType?) -> Void) -> Self { return response(queue: queue) { (request, response, data, error) -> Void in dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments) let result = JSONResponseSerializer.serializeResponse(request, response, data, error) 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 ?? response.data, result.error) // Here it shows the error: Cannot convert value of type 'T?' to expected argument type '_?' } } } } 
+5
source share
2 answers

Just found a solution. This seemed to be a problem with the Xcode 7.1 beta compiler. This gave a problem to the "parsedObject" parameter and there was an error with the next parameter.

 completionHandler(self.request!, self.response, parsedObject, **result.value ?? data**, result.error) 

So, if you happen to get the same error, review all other parameters in order.

Good luck.

+14
source

Update for mabril answer for Alamofire 3.0

 completionHandler(response.request!, response.response, parsedObject, response.result.value ?? response.data, response.result.error) 
0
source

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


All Articles