TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Error -- Generic parameter 'T' could not be inferred?

Forums > SwiftUI

Somewhere in your code you very precisely define a function named getData

Here's your definition:

func getData<T>(url: String, data: (T?, String?) -> ()) where T : Decodable { // details removed }

You’re telling Swift that at some point you will want to call getData, where it retrieves objects of type Cars. In another part of the app you may call getData and retrieve objects of type BroadwayShows.

In short, you are saying getData should work with any Type (shorthand “T”) as long as that type conforms to the Decodable protocol.

But when you use the getData function in code, you have to provide a hint to the compiler. I think you’re missing the hint and Swift tries, but fails, to guess (infer) the data type.

Try this?

// Use my cool getData function!  Provide a hint!
self.getJson.getData<Card>(url: Urls.new.rawValue) { news, error in 
            print("error")
        }

Nota bene: fetch! not featch.

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.