'Swift Generics Generic parameter could not be inferred

I'm trying to create a protocol that has a function with generic parameters.

protocol APIRequest {
    static func fetchData<T: Codable>(completion: @escaping(T?, NetworkError?) -> Void)
}

then I have a struct that conforms the protocol

static func fetchData<Ztar: Codable>(completion: @escaping (Ztar?, NetworkError?) -> Void) {
        let url = URLConstructor.url(scheme: "https", host: "swapi.dev" , path: "/api")
        guard let url = url else { return }
        let task = URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data else {
                completion(nil, NetworkError.badResponse)
                return
            }
            do {
                let decoder = JSONDecoder()
                let object = try decoder.decode(Ztar.self, from: data)
                completion(object, nil)
            } catch {
                print(error)
            }
        }
        task.resume()
    }

but I'm not sure if that implementation of the type of the generic is correct because in my ViewController I'm receiving the error Generic parameter 'Ztar' could not be inferred

NetworkManager.fetchData { star, error in
            
}

Can someone explain What I'm doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source