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

2

5

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

}
Dromond answered 2/10, 2015 at 11:59 Comment(0)
D
14

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.

Dromond answered 2/10, 2015 at 14:46 Comment(2)
Ha ha. Luckily I saw your answer early on. This is the kind of bug that can take hours to debug thanks to swifts helpful error messages. Thanks a lotErme
wow thanks, the error was on next parameter for me too (running Xcode 8.0)Maurine
F
0

Update to mabril's answer for Alamofire 3.0

completionHandler(response.request!, response.response, parsedObject, response.result.value ?? response.data, response.result.error)
Fleece answered 8/9, 2016 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.