Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Unable to infer type of a closure parameter 'b' in the current context". Getting this error while calling the function

getting error while calling the function.

func hitService<T : Codable>(urlS: String , completion : @escaping (T) -> Void) {
    
    guard let url = URL(string: urlS) else {return}
    
    
    let session = URLSession.shared
    
    let _ = session.dataTask(with: url) { dt, resp, err in
        
        
        let decoder = JSONDecoder()
        
        if let d = dt {
            
            do {
                let obj = try decoder.decode(T.self, from: d)
                completion(obj)
            } catch {print(error.localizedDescription)}
        }
    }.resume()
}

calling function like this and getting error on it.
I’ve tried passing a data type inside <> as well.

 hitService(urlS: urlStr) { b in
        
        
        
  }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The generic function relies on you specifying a type when it’s called so it can infer the types it uses.

In this case you’ll need to supply the type of the closure parameter b i.e.

hitService(urlS: urlStr) { (b: MyType) in
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading