简体   繁体   English

从 'AFError?' 到不相关的类型 'URLError' 总是失败警告

[英]Cast from 'AFError?' to unrelated type 'URLError' always fails warning

I'm getting this warning Cast from 'AFError?' to unrelated type 'URLError' always failsCast 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.我尝试使用NSErrorError类但没有成功。

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 默认返回不能转换为URLErrorAFError实例。 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.

相关问题 从[String:AnyObject]强制转换为不相关的类型NSMutableDictionary始终失败警告 - cast from [String:AnyObject] to unrelated type NSMutableDictionary always fails Warning 从“字符串”强制转换为不相关的类型“ LocationModel”始终失败警告 - Cast from 'String' to unrelated type 'LocationModel' always fails warning 从&#39;()&#39;转换为不相关的类型&#39;String&#39;总是失败 - Cast from '()' to unrelated type 'String' always fails Swift:从“Ride”转换为不相关的类型“NSDictionary”总是失败 - Swift: Cast from 'Ride' to unrelated type 'NSDictionary' always fails 从“ NSPersistentStoreResult”强制转换为无关类型“ [entity]”始终失败 - Cast from 'NSPersistentStoreResult' to unrelated type '[entity]' always fails 从“ [任何]?”演员表 到不相关的类型&#39;[String:String?]&#39;总是失败 - Cast from '[Any]?' to unrelated type '[String : String?]' always fails issue 来自'FIRRemoteConfigValue!'不相关的类型'String'总是失败 - Cast from 'FIRRemoteConfigValue!' to unrelated type 'String' always fails Swift-从“ NSData”投射? 无关类型“ NSDictionary”总是失败 - Swift - Cast from 'NSData?' to unrelated type 'NSDictionary' always fails 从Response转换为不相关的类型Dictionary始终失败Alamofire 3.4 - Cast from Response to unrelated type Dictionary always fails Alamofire 3.4 从'Int?'演员不相关的类型'NSNumber'总是失败 - Cast from 'Int?' to unrelated type 'NSNumber' always fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM