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