无法使用swift中的类型参数列表调用Method

时间:2016-04-28 06:05:36

标签: ios swift generics

我有一个功能

func mapResponse<T:Mappable>(responseObject:Alamofire.Request, 
                   forRequest requestObject:RequestObject,
                              responseType:T,
                                completion:(AnyObject?,String?) -> Void) 
{
    responseObject.responseObject{(response: Response<T, NSError>) in
        if let error = response.result.error {
                       completion(nil,error.description)
                       return
                }
                else {
                    let value:T = response.result.value!
                    self.saveObject(value, forRequest: requestObject,completion: completion)
                }
            }
}

当我称之为

let model:Mappable = requestObject.responseModel
ResponseMapper.sharedInstance.mapResponse(request, 
                                           forRequest:requestObject,
                                         responseType:model,
                                           completion:completion)

,其中

struct RequestObject { 
    let requestName:RequestName 
    let parametersStructure:Array<ParameterKey>? 
    let inputParameters:Dictionary<Parameter,AnyObject>? 
    let requestType:RequestType 
    let requestUrl:String 
    let cachLevel:CachLevel 
    let responseModel:Mappable 
    let parameterEncoding:ParameterEncoding
}

它给我错误:

  

“无法使用参数列表”

调用'方法'

虽然它的工作正常如下

ResponseMapper.sharedInstance.mapResponse(request, 
                                        forRequest:requestObject,       
                                      responseType:CarModel(), 
                                        completion:completion)

,其中

Class CarModel: Mappable

第一次通话的问题在哪里

1 个答案:

答案 0 :(得分:0)

requestObject.responseModel应符合Mappable

 struct RequestObject<T where T:Mappable> {
    let responseModel:T
 }