swift 无法推断泛型参数T,问题

eh57zj3b  于 2023-02-15  发布在  Swift
关注(0)|答案(1)|浏览(195)

下面是我的函数声明....

func requestApi<T:Codable>(_ url: URLConvertible,method:HTTPMethod = .post, parameters: Parameters, header:[String:String] ,completion:@escaping (_ success:Bool,_ result:T?) -> Void) {
    
    print("parameters: ",parameters)
    print("HEADERS: ",header)
    requestAPI(url, method:method,httpBody:parameters.queryString, headers: header) { (result:ApiResult<T,APIError>) in
        print(result)
        switch result {
        case .success1(let result):
            print(result)
            completion(true, result)
        case.failure(let error):
            DataHandler.shared.showAlert(title: "", message: error.customDescription)
            print(error.customDescription)
            completion(false, nil)
        }
    }
}

这是我调用这个函数的方法。

UserStore.shared.requestApi(CUSTOMER_BOOKING_DETAILS, parameters: params, header: header) { (flag, result) in ...

在函数调用时,我收到此错误“无法推断泛型参数T,问题”

798qvoo8

798qvoo81#

你应该在完成块中给予结果类型

UserStore.shared.requestApi(parameters: params, header: header) { (flag, (result as? CUSTOMER_BOOKING_DETAILS)) in ...

UserStore.shared.requestApi(parameters: params, header: header) { (flag, (result: CUSTOMER_BOOKING_DETAILS)) in ...

相关问题