Argument for Generic Parameter Could Not Be Inferred

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.

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)
} else {
signOut()
}
}
}

For more info go here

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)

Generic parameter 'C' could not be inferred

Right now, you're trying to iterate through a Dictionary using ForEach, which doesn't work -- ForEach expects a RandomAccessCollection, which is most often, an Array.

Instead, you can iterate over just the keys of the Dictionary.

struct ContentView: View {

var data: [Data] = [data1, data2]

var body: some View {
let grouped = groupByDate(data)
List {
ForEach(Array(grouped.keys), id: \.self) { key in
let studentsInMonth = grouped[key]!
Section(header:Text(key, style: .date)) {
ForEach(studentsInMonth, id:\.self) { item in
HStack {
Text(item.name)
padding()
Text(item.date, style: .time)
if(item.paymentStatus == false) {
Image(systemName: "person.fill.questionmark")
.foregroundColor(Color.red)
} else {
Image(systemName: "banknote")
.foregroundColor(Color.green)
}
}
} // ForEach ends here...
} // section ends here
} // ForEach ends here
} // List ends here
}
}

Although the above works, I'd consider refactoring your groupByDate function so that it returns an Array directly instead of having to go through the above transformations.

Also, unrelated to your question, but right now, in your header, you're displaying a date -- you get just the year and month, but then display it as a full date, so the header says "January 1, 2022" for all the January dates. You probably want to refactor this to just show the month and year.

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 on extension

You actually don’t need a generic for this, just constraint the extension to only work with arrays of Hashable:

extension Array where Element: Hashable {
func asCast() -> [ASValue<Element>]{
self.map({
ASValue(item: $0)
})
}
}

Void withCheckedThrowingContinuation Generic parameter 'T' could not be inferred

As I was writing this I found being explicit like this works:

    public
func
write(toDevice inDeviceID: Int, atAddress inAddr: Int, value inVal: Float)
async
throws
{
try await withCheckedThrowingContinuation
{ (inCont: CheckedContinuation<Void, Error>) -> Void in
self.workQ.async
{
do
{
self.deviceID = inDeviceID
try self.write(address: inAddr, value: inVal)
inCont.resume()
}

catch (let e)
{
inCont.resume(throwing: e)
}
}
}
}

I filed a bug with Apple.

Generic parameter 'T' could not be inferred when calling function

When you call a function, the swift compiler must be able to infer every generic parameter.

If you pass nil, the generic parameter cannot be inferred, because it is compatible with every optional type.

You must tell it that this nil is of a certain type. You can do this by casting:

let vc = ShowUserInfoVC.create(userId: id, streamId: id, delegateToController: nil as SomeType?)

As Alexander in the comments suggested, SomeType?.none and Optional<SomeType>.none work as well

where SomeType is a type that satisfies the constraints.

That sucks, doesn't it?

A workaround for this is to create an overload of create that takes only 3 arguments, and have that call create as shown above.

For example:

static func create<T>(userId: Int, streamId: Int, isPushStream: Bool = false) -> ShowUserInfoVC where T: UIViewController, T: ShowUserInfoVCDelegate {
create(userId: userId, streamId: streamId, isPushStream: isPushStream, delegateToController: nil as DummyController?)
}

// private/fileprivate "dummy" class
private class DummyController: UIViewController, ShowUserInfoVCDelegate {
// implement methods with stubs
}


Related Topics



Leave a reply



Submit