I have a model class Response<T>
constructed as below:
class Response<T>: Codable where T: Codable {
var code: Int?
var data: T?
var message: String?
}
I’d like to make a swift combine extension that type constraints to the Response<T>
class. I’d like to create a extended func to unwrap the data from my backend response and throw error if there is any.
My last approach is like this:
extension Publisher where Output == Response<T>, T: Codable {
func extractData() -> AnyPublisher<T, APIError> {
return tryMap { response in
guard let data = response.data else {
throw APIError.dataNotFound
}
return data
}
.eraseToAnyPublisher()
}
}
enum APIError: Error {
case dataNotFound
// Other error
}
After wrote that code, I get error Cannot find type 'T' in scope
in line where T type is typed.
How to get rid of the error and get the functionality as I need?
Thank you in advance.