我目前正在学习如何实现泛型函数。我正在尝试创建一个函数,它将返回一个泛型类型的回调
这是我的代码
static func performPOST< T:…
因为会有很多API调用会返回DomainObjectWrapper。所以我想创建一个通用函数将帮助我避免冗余代码。谢谢
如果是这样,那么你会使用 DataResponse<DomainObjectWrapper<T>> 代替 DataResponse<DomainObjectWrapper<MenuItemDO>> 在Alamofire调用的closure参数中:
DataResponse<DomainObjectWrapper<T>>
DataResponse<DomainObjectWrapper<MenuItemDO>>
static func performPOST<T: DomainEntity>(action: Module, completion: @escaping (CallbackResponse, DomainObjectWrapper<T>?) -> Void) { //logic implementation at first switch action { // this line self.alamofireManager.request(urlRequest, encoding: URLEncoding.default, headers: headers).responseObject { (response: DataResponse<DomainObjectWrapper<T>>) in // <----- this line var validateRequestResponse = RestHelper.validateRequestResponse(response: response) let responseObject = validateRequestResponse.responseObject if validateRequestResponse.isSuccess {} else { if let errorMessage = responseObject?.error { validateRequestResponse.message = errorMessage } else if let warningMessage = responseObject?.warning { validateRequestResponse.message = warningMessage } } callbackResponse = RestHelper.bindValidateRequestResponse(validateRequestResponse: validateRequestResponse) completion(callbackResponse, responseObject) } } }