Search code examples
iosswiftgenericsalamofireobjectmapper

AlamofireObjectMapper Generic parameter 'T' could not be inferred


i'm trying to have a system where all my api requests path through one function and map them to the corresponding object

func sendApi<T>(url : String , httpMethod : HTTPMethod = .get,
                 parameters: Parameters? = nil,
                 encoding: ParameterEncoding = URLEncoding.default,
                 headers: HTTPHeaders? = nil , callbackSuccess : @escaping (T) -> () , callbackFailure : @escaping (T) -> ()) where T : Mappable {

    Alamofire.request(url, method: httpMethod, parameters: parameters , encoding: encoding, headers: headers).responseObject{(response: (DataResponse<T>))in

            switch response.result {

            case .success :
                let result  = Mapper<T>().map(JSONObject: response.value)!
                callbackSuccess(result)
                break;
            case .failure(let error):
                if((error as NSError).code == ErrorResponse.noInternetConnection){
                    //  errorCallBack(ErrorResponse.noInternetConnectionString)
                }
                // errorCallBack(error.localizedDescription)
                print(error.localizedDescription)
                break;
            }
    }
}

but when i try to call the function inside a get method for example

 func testApiGet(url: String , packageId : Int ,callback :@escaping (myObject) -> Void , errorCallBack : @escaping (String) -> Void ){

        let token = spm.getUserToken()
        let headers = ["X-Auth-Token" : token]
        let newUrl = url + "?packageId=" + String(packageId)

        sendApi(url: url, httpMethod: HTTPMethod.get , parameters: nil, encoding: JSONEncoding.default, headers: headers, callbackSuccess: {(jsonObject) in

        } , callbackFailure:{ (jsonObject)in

        })        
    }

i get the error "Generic parameter 'T' could not be inferred"

of course i can set the type of the object

(response: (DataResponse<myObject>)

and the error will go.

my question is how solve this error to make it fully generic


Solution

  • It does not make sense that your error handler and your success handler have the same type T: Mappable. You are only going to get a mapped object in the success handler. The error handler should be something like (Error) -> ().

    Secondly your function is generic with respect to the parameter type of the closure arguments, but when you are calling the function you are not specifying the type:

     sendApi(url: url, httpMethod: HTTPMethod.get , parameters: nil, encoding: JSONEncoding.default, headers: headers, callbackSuccess: {(jsonObject) in
    
            } , callbackFailure:{ (jsonObject)in
    
            })  
    

    The compiler therefore has no idea what type you are expecting and thats why its complaining that it cannot infer the type here. You need to explicitly provide the parameter type because that is what determines what version of the generic gets invoked:

     sendApi(url: url, httpMethod: HTTPMethod.get , parameters: nil, encoding: JSONEncoding.default, headers: headers, callbackSuccess: {(jsonObject: MyMappableType) in
    
            } , callbackFailure:{ (error: Error)in
    
            })