i have 2 main parts in my app infrastructure.
NetworkingManager
NetworkRequest
My goal is to have the request hold its Codable
Type
so when networking is done the Manager
layer can instantiate new instance with the correct type by using
decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable
so my NetworkRequest
look like so
class NetworkRequest {
var objectClass:Codable.Type
}
lets say i have a Person
class which conforms to Codable
class Person : Codable {
var name:String?
}
now i need to assign the type into the request like so (in an ugly way to get to the point here)
let request = NetworkingRequest()
request.objectClass = Person.self
now when i want the NetworkingManager
to decode the response i use it like so:
JSONDecoder().decode(type:request.objectClass, data:dataFromService)
the problem is when i do so i get this error:
Cannot invoke decode with an argument list of type (Decodable.Type, from: Data). Expected an argument list of type (T.Type, from: Data).
any help would be appreciated