从 'AFError?' 转换为无关类型 'URLError' 总是失败警告
Cast from 'AFError?' to unrelated type 'URLError' always fails warning
当我尝试在以下函数中转换错误时收到此警告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)
}
在这部分syncRequest?.didFail(with: .PurchaseValidationError(code, dataResponse?.error as? URLError))
我尝试使用 NSError
或 Error
类 但没有成功。
谁能告诉我如何摆脱这个警告?
提前致谢
我遇到了和你类似的问题。
我做了一个模型如下:
struct MyErorr: Error {
public var errorCode: Int
public var erorrMessage: String
}
并从 myError 这个错误部分创建对象,如下所示:
// error instance from AFError
if error.responseCode == 401 {
let myError: MyErorr = .init(errorCode: 401, erorrMessage: "Unauthorized")
callback(myError)
} else { ... }
希望对你有用
解决方案是添加另一个演员表。
所以,而不是
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as? URLError))
我做的案例如下
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as NSError? as? URLError))
Alamofire returns AFError
默认情况下无法转换为 URLError
的实例。您可以检查 AFError
文档或源代码以获取完整详细信息,但网络故障等潜在错误会通过 underlyingError
属性 暴露出来。所以在网络故障的情况下,response?.error?.underlyingError as? URLError
应该给你正确的值,但只有 if 潜在的错误实际上是 URLError
.
最终我建议您直接处理 AFError
,因为它是 Alamofire 可以 return 的所有错误的唯一完整表示。要么,要么你自己的错误类型为你处理。转换不是捕获这些错误的正确方法,因为任何转换都会丢失一些错误信息。
当我尝试在以下函数中转换错误时收到此警告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)
}
在这部分syncRequest?.didFail(with: .PurchaseValidationError(code, dataResponse?.error as? URLError))
我尝试使用 NSError
或 Error
类 但没有成功。
谁能告诉我如何摆脱这个警告?
提前致谢
我遇到了和你类似的问题。 我做了一个模型如下:
struct MyErorr: Error {
public var errorCode: Int
public var erorrMessage: String
}
并从 myError 这个错误部分创建对象,如下所示:
// error instance from AFError
if error.responseCode == 401 {
let myError: MyErorr = .init(errorCode: 401, erorrMessage: "Unauthorized")
callback(myError)
} else { ... }
希望对你有用
解决方案是添加另一个演员表。 所以,而不是
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as? URLError))
我做的案例如下
syncRequest?.didFail(with: .PurchaseValidationError(code,
dataResponse?.error as NSError? as? URLError))
Alamofire returns AFError
默认情况下无法转换为 URLError
的实例。您可以检查 AFError
文档或源代码以获取完整详细信息,但网络故障等潜在错误会通过 underlyingError
属性 暴露出来。所以在网络故障的情况下,response?.error?.underlyingError as? URLError
应该给你正确的值,但只有 if 潜在的错误实际上是 URLError
.
最终我建议您直接处理 AFError
,因为它是 Alamofire 可以 return 的所有错误的唯一完整表示。要么,要么你自己的错误类型为你处理。转换不是捕获这些错误的正确方法,因为任何转换都会丢失一些错误信息。