Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'T?' to expected argument type '_?' - Generic types and completion blocks

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

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

I've come to this piece of code and now i'm stuck.

It seems that the Generic Type T is not accesible inside the completion block of the response. Is a Alamofire 3 change or a Swift 2.1 change?

This is the error:

Cannot convert value of type 'T?' to expected argument type '_?'

  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 '_?' 
            }
        }
    }

}
like image 956
mabril Avatar asked Oct 02 '15 11:10

mabril


1 Answers

Just found the solution. It seemed to be a problem of the Xcode 7.1 beta compiler. It was giving the problem on the "parsedObject" parameter and there was a mistake on 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 the other parameters are ok.

Good luck.

like image 70
mabril Avatar answered Nov 08 '22 22:11

mabril