在Swift 3错误中访问代码 [英] Accessing code in Swift 3 Error

查看:85
本文介绍了在Swift 3错误中访问代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新增功能,NSError被桥接到Swift Error协议类型.处理失败的SKPaymentTransaction时,这会影响StoreKit.您应该检查以确保没有发生错误,因为交易被取消了才知道是否向用户显示错误消息.您可以通过检查错误的code来执行此操作.但是,使用Error而不是NSError时,没有定义code.我一直无法弄清楚如何从Error正确获取错误代码.

New in Xcode 8 beta 4, NSError is bridged to the Swift Error protocol type. This affects StoreKit when dealing with failed SKPaymentTransactions. You ought to check to be sure the error didn't occur because the transaction was cancelled to know whether or not to show an error message to the user. You do this by examining the error's code. But with Error instead of NSError, there is no code defined. I haven't been able to figure out how to properly get the error code from Error.

这在Swift 3的早期版本中有效.

This worked in the previous version of Swift 3:

func failedTransaction(_ transaction: SKPaymentTransaction) {
    if let transactionError = transaction.error {
        if transactionError.code != SKErrorCode.paymentCancelled.rawValue {
            //show error to user
        }
     }
     ...
}

现在errorError而不是NSErrorcode不是成员.

Now that error is an Error not NSError, code is not a member.

推荐答案

在xCode 8和Swift 3中,铸造到SKError似乎对我有用……

Casting to SKError seems to be working for me in xCode 8 and Swift 3...

    guard let error = transaction.error as? SKError else {return}
    switch error.code {  // https://developer.apple.com/reference/storekit/skerror.code
    case .unknown: break
    case .paymentCancelled: break
    case .clientInvalid: break
    case .paymentInvalid: break
    case .paymentNotAllowed: break
    case .cloudServiceNetworkConnectionFailed: break
    case .cloudServicePermissionDenied: break
    case .storeProductNotAvailable: break
    }

不需要rawValue.

这篇关于在Swift 3错误中访问代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆