调用方法时无法推断通用参数 'T'
Generic parameter 'T' could not be inferred while calling a method
我遇到了以下错误
无法推断通用参数 'T'
我已经创建了一个方法,当我尝试调用该方法时出现了该错误。我在下面添加了这两种方法。
func requestNew<T> ( _ request: URLRequest, completion: @escaping( Result< T , NetworkError>) -> Void ) where T : Decodable {
URLCache.shared.removeAllCachedResponses()
print("URL \((request.url as AnyObject).absoluteString ?? "nil")")
//use the currentrequest for cancel or resume alamofire request
currentAlamofireRequest = self.sessionManager.request(request).responseJSON { response in
//validate(statusCode: 200..<300)
if response.error != nil {
var networkError : NetworkError = NetworkError()
networkError.statusCode = response.response?.statusCode
if response.response?.statusCode == nil{
let error = (response.error! as NSError)
networkError.statusCode = error.code
}
//Save check to get the internet connection is on or not
if self.reachabilityManager?.isReachable == false {
networkError.statusCode = Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue)
}
completion(.failure(networkError))
}else{
print("response --- > ",String(data: response.data!, encoding: .utf8) ?? "No Data found")
if let responseObject = try? JSONDecoder().decode(T.self, from: response.data!) {
completion(.success(responseObject.self))
}else {
}
}
}
}
错误截图如下
![
]1
func getVersion1(complete :@escaping (Response<Version>) -> Void, failure:@escaping onFailure) {
self.network.requestNew(self.httpRequest) { (result) in
print("hello")
}
当Swift无法推断泛型参数时,虽然它接受方法的泛型声明,但可以通过传递类型固定参数来指定类型
试试这个:
func getVersion1(complete :@escaping (Response<Version>) -> Void, failure:@escaping onFailure) {
self.network.requestNew(self.httpRequest) { (result: Result<Version, NetworkError>) in
print("hello")
}
}
如果 Version
不是您期望的请求类型,您可能需要将 Result<Version, NetworkError>
更改为 Result<SomeDecodableType, NetworkError>
。
我遇到了以下错误 无法推断通用参数 'T'
我已经创建了一个方法,当我尝试调用该方法时出现了该错误。我在下面添加了这两种方法。
func requestNew<T> ( _ request: URLRequest, completion: @escaping( Result< T , NetworkError>) -> Void ) where T : Decodable {
URLCache.shared.removeAllCachedResponses()
print("URL \((request.url as AnyObject).absoluteString ?? "nil")")
//use the currentrequest for cancel or resume alamofire request
currentAlamofireRequest = self.sessionManager.request(request).responseJSON { response in
//validate(statusCode: 200..<300)
if response.error != nil {
var networkError : NetworkError = NetworkError()
networkError.statusCode = response.response?.statusCode
if response.response?.statusCode == nil{
let error = (response.error! as NSError)
networkError.statusCode = error.code
}
//Save check to get the internet connection is on or not
if self.reachabilityManager?.isReachable == false {
networkError.statusCode = Int(CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue)
}
completion(.failure(networkError))
}else{
print("response --- > ",String(data: response.data!, encoding: .utf8) ?? "No Data found")
if let responseObject = try? JSONDecoder().decode(T.self, from: response.data!) {
completion(.success(responseObject.self))
}else {
}
}
}
}
错误截图如下 ![ ]1
func getVersion1(complete :@escaping (Response<Version>) -> Void, failure:@escaping onFailure) {
self.network.requestNew(self.httpRequest) { (result) in
print("hello")
}
当Swift无法推断泛型参数时,虽然它接受方法的泛型声明,但可以通过传递类型固定参数来指定类型
试试这个:
func getVersion1(complete :@escaping (Response<Version>) -> Void, failure:@escaping onFailure) {
self.network.requestNew(self.httpRequest) { (result: Result<Version, NetworkError>) in
print("hello")
}
}
如果 Version
不是您期望的请求类型,您可能需要将 Result<Version, NetworkError>
更改为 Result<SomeDecodableType, NetworkError>
。