我怎样才能在 Swift 中解决 "Generic parameter 'T' could not be inferred"

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

我有以下结构和函数。

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
}

我想像那样调用 postAsync func 但它说 **Generic parameter 'TResponse' could not be inferred** 我如何调用这个方法?我尝试了不同的方法,但没有解决。

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

我不确定您尝试的解决方案为何失败,也许它们不适用于 async/await 功能。

也就是说,虽然通常可以使用隐式泛型,但 Swift 中更常见的做法是将类型作为参数之一,而不是将其保持隐式。在 类 中就是这样做的,比如 JSONDecoder

我建议将您的函数签名更改为:

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

并这样称呼它:

let res = await postAsync(route: "MyController/Get", request: someRequest, receive: SomeResponse.self)

您的函数不是 return SomeResponse,它 return 是 SomeResponse?,所以您的意思是:

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

let res: SomeResponse? = ...

我同意 EmilioPaleaz 关于如何改进 API 的观点,但我建议添加一个默认值,这样可以两全其美:

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

有了这个,当return类型已知时,你可以省略它。