Search code examples
iosswiftalamofire

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


I'm getting this warning Cast from 'AFError?' to unrelated type 'URLError' always fails when I try to cast the error in the following function

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))

I tried to use NSError or Error classes but no success.

Can anyone let me know how I can get rid of this warning?

Thanks in advance


Solution

  • 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))