我有以下结构和功能。
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)