我遇到了错误
模式不能匹配'SKError'类型的值
但是,当我使用存储套件查找SKError
的代码时,我正确输入了错误并且找不到解决方案。 (获取4个错误-模式在每种情况下都不能匹配'SKError'类型的值-开关错误中的每种情况。)
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))
}
})
}
答案 0 :(得分:1)
我推断您的PurchaseResult
类似于:
typealias PurchaseResult = CustomResult<Product, SKError>
(我不知道您的Result
类型是什么,但我所知道的是标准Result
类型是.failure
或.success
,而您的{ {1}}代替.error
。)
我还假设您必须具有自己的错误类型,例如:
.failure
因此,我首先将enum MyAppError: Error {
case invalidProductId(String)
case noProductIdentifier
case paymentNotAllowed
}
更改为允许任何相关的错误类型:
PurchaseResult
然后处理各种类型的错误
typealias PurchaseResult = CustomResult<Product, Error>
如果使用的是Swift 5,请输入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.")
}
}
关键字。如果使用的是较早的Swift版本,则可以忽略。
顺便说一句,如果您想知道@unknown
的枚举,当我为SKError
的{{1}}做一个switch
时,它给了我一个“解决”的建议,并为我解决了所有这些问题。