Decode Array of Model
Hi, first of all thanks for your post, it's really a great example of doing networking layers. I'm struggling though to use it for returning array of objects conforming to Model protocols. When I've used a similar approach (a send function with generics) I used the Decodable instead of Model when defining the send function, this will allow me for example to write calls like that:
Network.shared.send(request) { (result: Result<[Output], Error>) in
without any errors. Now even if the class Output conforms to the Model protocols I receive the following error:
Instance method 'send(_:completion)' requires that '[Output]' conforms to 'Model'
I can't understand how to make it happen since the protocol is only a wrapper for Codable.
Thanks again and merry Christmas.
It sounds like it's because your function probably looks like:
send<T: Model>(_ request: Requestable, completion: @escaping (Result<T, Error>)->Void) -> NetworkTask
But in your code here, you're asking for [T] instead of T in the method. There are a few ways I could think of around this. The easiest is probably to also add a function which returns a Result<[T], Error> object. Or, you could define a wrapper model like:
struct ResultsArray<T>: Model {
let elements: [T]
}