ios – Add Generic item in array in Swift


I want to add a generic item in an array. How do I achieve it in following method:

    func get<T: Decodable>(from url : URL, type: T.Type, completion : @escaping (Result<T, ApiError>) -> ()) {
//            messages.append((url, completion))
        }
        
        var messages : [(url: URL, completion: (Result<Codable, Error>) -> ())] = []
        

I am getting following error when I try to append:

Cannot convert value of type ‘(URL, (Result<T, ApiError>) -> ())’ to
expected argument type ‘(url: URL, completion: (Result<any Decodable,
ApiError>) -> ())’

I am using get function to return api response as a generic model and trying to use above code in my test cases.
I believe I need to specify that array is expecting a generic item but I am not sure how to achieve it.

Edit:
original function for which I am writing tes case:

public func getTimeTable() -> AnyPublisher<TimetableModel, Error> {
    Future { [weak self] promise in
        guard let self = self else { return }
        
        httpClient.get(from: url, type: TimetableModel.self) { result in
            switch result {
            case .success(let timeTable):
                promise(.success(timeTable))
            case .failure(let error):
                promise(.failure(error))
            }
        }
    }.eraseToAnyPublisher()
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img