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

}
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
mabril
  • 191
  • 1
  • 7

2 Answers2

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.

mabril
  • 191
  • 1
  • 7
  • 4
    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 lot – villy393 Jan 12 '16 at 08:42
  • wow thanks, the error was on next parameter for me too (running Xcode 8.0) – aryaxt Sep 29 '16 at 22:22
0

Update to mabril's answer for Alamofire 3.0

completionHandler(response.request!, response.response, parsedObject, response.result.value ?? response.data, response.result.error)
jscs
  • 63,694
  • 13
  • 151
  • 195
George
  • 119
  • 2
  • 2