I am using the object mapper for swift and have run into a problem.
I have a API class which does a call to the server and responds with a json response.
Now when i call the dataTask function in this API class, i want to pass through the class type i want the json response to be mapped to using Object mapper.
func dataTask(method: String, type: AnyClass) {
Mapper<AnyClass>().map(response)
}
I am not sure how I can pass in the class to be used with the mapper, I have tried generics but not sure if i implemented it correctly or not.
func dataTask<T>(method: String, type: T.type) {
}
Any help would be appreciated.
Thanks
You need a function that's generic over any Mappable
type. Essentially, this will produce one version of dataTask(method:_:)
for every type T
you use it with, which will call the appropriate version of Mapper()
.
func dataTask<T: Mappable>(method: String, _: T.Type) {
Mapper<T>().map(response)
}
Note the second argument, in order to make it as a generic type its needed to add ".Type".