简体   繁体   中英

Trouble casting Error from transaction in SKPaymentTransaction (Swift 3)

I am trying to get to the error code in a transaction (Swift 3):

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            if let errorCode = (transaction.error as NSError).code {
             //Do something
            }
        }


}

I am getting the below error.

'Error?' is not convertible to 'NSError'; did you mean to use 'as!' to force downcast?`.

Why wont this work? I thought Error could cast to NSError in Swift 3.

Try to cast the Error object to NSError then access error code.

if let nsError = error as? NSError {
    print(nsError.code)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM