tristanhimmelman/AlamofireObjectMapper

Passing class type to generic

Closed this issue · 2 comments

In the following code how can i pass a class to responseType ?

func sendGetRequest(url:String,
                   request: String,
                   responseType: AnyClass,
                  completion: (AnyObject?, ServerError?)-> Void) {
Alamofire.request(.GET, url, parameters: nil, encoding: .JSON, headers: headers)
      .validate()
      .responseObject { (response: Response< **responseType** , NSError>) in

...

}

Currently AnyClass is not working it says "responseType is not a type"
If i replace responseType with actual class it works. But i want to make the function generic and responseType can be of any class to map properly.

func sendGetRequest<T: Mappable>(url:String,
                   request: String,
                  completion: (T?, ServerError?)-> Void) {
Alamofire.request(.GET, url, parameters: nil, encoding: .JSON, headers: headers)
      .validate()
      .responseObject { (response: Response<T , NSError>) in

Something like this should do it

Hi guys... What about if we are using .responseJson instead of .responseObject... I have a problem in my code:

func get <T> (type: T.Type, headers:[String:String], onCompletion: @escaping ServiceResponse) where T:Mappable, T:Meta {

    let getManager = Alamofire.request(self.url + type.url(), method: .get, parameters: nil, headers: headers)
    getManager.validate()

    getManager.responseJSON{ (response: DataResponse<[T]>) in

The error is Cannot convert value of type '(DataResponse<[T]>) -> ()' to expected argument type '(DataResponse) -> Void'

If I change responseJSON to responseArray it doesn´t work in runtime because the server returns JSON but it compiles.

Thanks, I appreciate your help.