I'm playing with Combine to learn it and improve my reactive programming skills, and I'm trying to create some generic class that convert data to my T type
I have this error, and I don't understand why
Key path value type '[T]' cannot be converted to contextual type 'T'
class Fetcher<T: Codable>: ObservableObject {
private var task: AnyCancellable?
@Published var result = [T]()
init<T: Codable> (type: T.Type) {
guard let url = URL(string: "https://api.example.com") else { return }
task = URLSession.shared.dataTaskPublisher(for: url)
.map{$0.data}
.decode(type: T.self, decoder: JSONDecoder())
.receive(on: DispatchQueue.global(qos: .background))
.replaceError(with: T.self as! T)
.assign(to: \.result, on: self)
}
}