模式无法匹配 'SKError' 类型的值

Pattern cannot match values of type 'SKError'

我遇到错误

Pattern cannot match values of type 'SKError'

但是,当我使用商店工具包查找 SKError 的代码时,我输入的错误是正确的,但找不到解决方案。 (出现 4 个错误 - 模式无法匹配 'SKError' 类型的值 - 对于 switch 错误中的每种情况)。

func alertForPurchaseResult(result : PurchaseResult) -> UIAlertController {
    switch result {
    case .success(let product):
        print("Purchase Succesful: \(product.productId)")

        return alertWithTitle(title: "Thank You", message: "Purchase completed")
        break
    case .error(let error):
        print("Purchase Failed: \(error)")
        switch error {
        case .failed(let error):
            if (error as NSError).domain == SKErrorDomain {
                return alertWithTitle(title: "Purchase Failed", message: "Check your internet connection or try again later.")
            }
            else {
                return alertWithTitle(title: "Purchase Failed", message: "Unknown Error. Please Contact Support")
            }
            break
        case .invalidProductId(let productID):
            return alertWithTitle(title: "Purchase Failed", message: "\(productID) is not a valid product identifier")
            break
        case .noProductIdentifier:
            return alertWithTitle(title: "Purchase Failed", message: "Product not found")
        break
        case .paymentNotAllowed:
            return alertWithTitle(title: "Purchase Failed", message: "You are not allowed to make payments")
            break

        }
        break
    }
}




func purchase(purchase: RegisteredPurchase){
       NetworkActivityIndicatorManager.NetworkOperationStarted()
       SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion: {
        result in
        NetworkActivityIndicatorManager.NetworkOperationFinished()

        if case .success(let product) = result {
            if product.needsFinishTransaction{
                SwiftyStoreKit.finishTransaction(product.transaction)
            }
            self.showAlert(alert: self.alertForPurchaseResult(result: result))
        }
    })
}

我推断你的 PurchaseResult 类似于:

typealias PurchaseResult = CustomResult<Product, SKError>

(我不知道你的 Result 类型是什么,但我只知道标准的 Result 类型是 .failure.success,而你的有 .error 而不是 .failure。)

我还假设您必须有自己的错误类型,例如:

enum MyAppError: Error {
    case invalidProductId(String)
    case noProductIdentifier
    case paymentNotAllowed
}

因此,我首先更改 PurchaseResult 以允许任何相关的错误类型:

typealias PurchaseResult = CustomResult<Product, Error>

然后处理各类错误

func alertForPurchaseResult(result: PurchaseResult) -> UIAlertController {
    switch result {
    case .success(let product):
        return alertWithTitle(title: "Thank You", message: "Purchase completed")

    case .error(let error as SKError):
        switch error.code {
        case .unknown:
            <#code#>
        case .clientInvalid:
            <#code#>
        case .paymentCancelled:
            <#code#>
        case .paymentInvalid:
            <#code#>
        case .paymentNotAllowed:
            <#code#>
        case .storeProductNotAvailable:
            <#code#>
        case .cloudServicePermissionDenied:
            <#code#>
        case .cloudServiceNetworkConnectionFailed:
            <#code#>
        case .cloudServiceRevoked:
            <#code#>
        case .privacyAcknowledgementRequired:
            <#code#>
        case .unauthorizedRequestData:
            <#code#>
        case .invalidOfferIdentifier:
            <#code#>
        case .invalidSignature:
            <#code#>
        case .missingOfferParams:
            <#code#>
        case .invalidOfferPrice:
            <#code#>
        @unknown default:
            <#code#>
        }

    case .error(let error as MyAppError):
        switch error {
        case .invalidProductId(let productID):
            return alertWithTitle(title: "Purchase Failed", message: "\(productID) is not a valid product identifier")

        case .noProductIdentifier:
            return alertWithTitle(title: "Purchase Failed", message: "Product not found")

        case .paymentNotAllowed:
            return alertWithTitle(title: "Purchase Failed", message: "You are not allowed to make payments")
        }

    case .error:
        return alertWithTitle(title: "Purchase Failed", message: "Unknown Error. Please Contact Support.")
    }
}

@unknown关键字,如果你使用Swift 5.如果使用更早的Swift版本,你可以省略它。

顺便说一句,如果您想知道 SKError 枚举,当我为 SKError.code 执行 switch 时,它给了我一个“修复”建议,它为我填充了所有这些。