Search code examples
swiftgenericsswiftuiswift5

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


I have following structs and functions.

struct ApiResponse<TResponse: Codable>: Codable {
    var isSuccess: Bool
    var mainResponse: TResponse?
}

public struct SomeResponse: Codable {
    var someProperty: String
}

public func postAsync<TRequest: Codable, TResponse: Codable>(route: String, request: TRequest) async throws -> TResponse? {

    let body = try JsonEncoder().encode(request)
    let urlRequest = createUrlRequest(route: route, method: "POST", body: body)

    let (data, _) = try await URLSession.shared.data(for: urlRequest)
    let apiResponse = try JsonDecoder().decode(ApiResponse<TResponse>.self, from: data)
    return response.mainResponse
}

I want to call postAsync func like that but it says **Generic parameter 'TResponse' could not be inferred** How can I call this method? I tried different ways but not solve.

 - let res = await postAsync(route: "MyController/Get", request: someRequest) as? SomeResponse
 - let res: SomeResponse = await postAsync(route: "MyController/Get", request: someRequest)

Solution

  • 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.