Generics in Swift - "Generic Parameter 'T' Could Not Be Inferred

Swift Generic parameter 'T' could not be inferred

I don't approve of the way you're doing this (on the general grounds that Any is just about always a bad smell), but here's a version that compiles (just delete the Any constraint):

class AppData {
static let genericDict: Dictionary<String, Any> = [:]

static func get<T>(_ objID: String) -> T {
let retVal: T
retVal = genericDict[objID] as! T
return retVal
}
}

class SomeClass {
let product : String = AppData.get("yoho")
}

Generic parameter 'T' could not be inferred - Swift 5.5

Actually my call was not good at all in the first place, this is how it should be done:

    // Gets User signed-in
func getUser() async throws -> AuthUser {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<AuthUser, Error>) in
if let user = Amplify.Auth.getCurrentUser() {
continuation.resume(returning: user)
return
} else {
signOut()
continuation.resume(with: .failure(YourCustomEnumError_or_the_actual_error))
return
}
}
}

For more info go here

Swift : Generic parameter 'T' could not be inferred

You're very close. Say you have a struct A that conforms to P. Then you could specify the generic parameter as follows:

var fp: A? = f()

Without that information, the compiler can't know what type fp should be.

How can I solve Generic parameter 'T' could not be inferred in Swift

Your function doesn't return SomeResponse, it returns SomeResponse?, so what you meant here is:

let res = ... as SomeResponse? // Note `as`, not `as?`

or

let res: SomeResponse? = ...

I agree with EmilioPaleaz about how to improve the API, though I would recommend adding a default value, which gives the best of both worlds:

... request: TRequest, returning: TResponse.Type = TResponse.self) async throws -> ...

With this, when the return type is known, you can omit it.

Swift Generics Generic parameter could not be inferred

It's not possible to implement this correctly. What type is Ztar? How would the compiler know? You've written "fetchData will fetch some kind of data, out of all the infinitely possible kinds of data in the universe, and you, the compiler, should decode it." That's not possible.

Instead, the way this would generally be written is:

fetchData<T: Codable>(ofType: T.Type, completion: @escaping (Result<T, NetworkError>) -> Void)

And then you would need to pass the type you expect as the first parameter:

fetchData(ofType: Record.self) { resultRecord in ... }

If you don't know what type Ztar is, how can the compiler?

(Note that you should almost never use (T?, Error?) as a type. That says "maybe T, maybe Error, maybe neither, maybe both." You almost always mean Result<T, Error> instead. That says "either T or Error.")

Swift generics issue: generic parameter could not be inferred

This is the wrong signature:

public func getLoginInfo(loginInfoClass: LoginInfoBase.Type, completionHandler: @escaping (Swift.Result<LoginInfoBase, Error>) -> Void)

You mean this:

public func getLoginInfo<T: LoginInfoBase>(loginInfoClass: T.Type, completionHandler: @escaping (Swift.Result<LoginInfoBase, Error>) -> Void)
^^^^^^^^^^^^^^^^^^ ^

You need to pass a concrete type to getLoginInfo that conforms to LoginInfoBase. Not just any subtype. This matches your genericQuery method.

You should then modify your call to genericQuery as:

genericQuery(urlString: "\(baseURL)/users/get_login_info/",
method: .get,
params: nil,
decodable: T.self) { ... } // use T.self here.

For more details, see Alexander's link.

Swift. Generic parameter 'T' could not be inferred

You shouldn't try to cast the value, but rather annotate test as TestModel? to let the compiler infer the generic type T as TestModel.

let test: TestModel? = testDecode(from: document.data)


Related Topics



Leave a reply



Submit