我正在尝试使用 Alamofire 3 和最新版本的 ObjectMapper ( https://github.com/Hearst-DD/ObjectMapperAlamofireObjectMapper
) 实现( https://github.com/tristanhimmelman/AlamofireObjectMapper )。
似乎 ,AlamofireObjectMapper
尚未更新为与 Alamofire 3 一起使用,所以我正在尝试自己做。
我来到了这段代码,现在我被卡住了。
似乎在响应的完成块内无法访问通用类型 T。是Alamofire
3 变化还是 Swift 2.1 变化?
这是错误:
无法转换“T”类型的值?到预期的参数类型“_?”
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 '_?'
}
}
}
}