[英]Cast from 'AFError?' to unrelated type 'URLError' always fails warning
I'm getting this warning Cast from 'AFError?' to unrelated type 'URLError' always fails
我
Cast from 'AFError?' to unrelated type 'URLError' always fails
Cast from 'AFError?' to unrelated type 'URLError' always fails
when I try to cast the error in the following function当我尝试在以下 function 中投射错误时,
Cast from 'AFError?' to unrelated type 'URLError' always fails
func requestBlock() {
struct ValidationConsumer: ResponseDelegate {
weak var syncRequest: Transfer_PurchaseValidation?
var productIdentifier: String
func didSucceed(_ _: JSON, _ _: AFDataResponse<Any>?) {
DDLogInfo("Purchase payload for productId = \(productIdentifier) was sent")
syncRequest?.didSucceed()
}
func didFail(_ json: JSON, _ code: Int?, _ dataResponse: AFDataResponse<Any>?) {
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as? URLError))
}
}
guard data.shouldBeTransferred else {
return
}
guard isUnderTest == nil else {
executeTestsequence()
return
}
guard let receiptDataString = data.receiptDataString,
let productIdentifier = data.productIdentifier else {
didFail(with: .InvalidData); return
}
let validationConsumer = ValidationConsumer(syncRequest: self,
productIdentifier: productIdentifier)
self.validatePurchase(receiptDataString, productIdentifier,
validationDelegate: validationConsumer)
}
at this part syncRequest?.didFail(with: .PurchaseValidationError(code, dataResponse?.error as? URLError))
在这部分
syncRequest?.didFail(with: .PurchaseValidationError(code, dataResponse?.error as? URLError))
I tried to use NSError
or Error
classes but no success.我尝试使用
NSError
或Error
类但没有成功。
Can anyone let me know how I can get rid of this warning?谁能让我知道如何摆脱这个警告?
Thanks in advance提前致谢
Alamofire returns AFError
instances by default which are not convertible to URLError
. Alamofire 默认返回不能转换为
URLError
的AFError
实例。 You can examine the AFError
documentation or source code for the full details, but underlying errors like network failures are exposed through the underlyingError
property.您可以查看
AFError
文档或源代码以获取完整的详细信息,但网络故障等底层错误通过underlyingError
错误属性公开。 So in the case of a network failure, response?.error?.underlyingError as? URLError
那么在出现网络故障的情况下,
response?.error?.underlyingError as? URLError
response?.error?.underlyingError as? URLError
should give you the correct value, but only if the underlying error is actually a URLError
. response?.error?.underlyingError as? URLError
应该为您提供正确的值,但前提是基础错误实际上是URLError
。
Ultimately I suggest you handle the AFError
directly, as it's the only full representation of all of the errors Alamofire can return.最后,我建议您直接处理
AFError
,因为它是 Alamofire 可以返回的所有错误的唯一完整表示。 Either that, or your own error type which does the handling for you.要么是那个,要么是你自己的错误类型,它为你处理。 Casting is not the correct way to capture these errors, as any cast will lose some error information.
强制转换不是捕获这些错误的正确方法,因为任何强制转换都会丢失一些错误信息。
I had a problem similar to yours.我遇到了和你类似的问题。 I made a model as follows:
我做了一个 model 如下:
struct MyErorr: Error {
public var errorCode: Int
public var erorrMessage: String
}
and create object from myError this error part like this:并从 myError 这个错误部分创建 object,如下所示:
// error instance from AFError
if error.responseCode == 401 {
let myError: MyErorr = .init(errorCode: 401, erorrMessage: "Unauthorized")
callback(myError)
} else { ... }
I hope it is useful for you我希望它对你有用
The solution was to add another cast.解决方案是添加另一个演员表。 So, instead of
所以,而不是
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as? URLError))
I did the case as following我做了以下案例
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as NSError? as? URLError))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.