Xcode 8 Beta 4, help with NSError / Error

I'm still wrapping my head around the migration away from NSError. In Xcode 8 Beta 3, here's how I detected a StoreKit error that was not a cancel:


// "transaction" is an SKPaymentTransaction
switch transaction.transactionState {
case .failed:
  if let code = transaction.error?.code, code != SKErrorCode.paymentCancelled.rawValue {
    // handle the error...


The above does not compile because the "error" property is no longer an NSError, which means it has no "code" property. I haven't been able to figure out how to migrate this code appropriately. Any help would be appreciated.

Answered by QuinceyMorris in 159175022

The b4 release notes have this example:


catch let error as CocoaError where error.code == .fileReadNoSuchFileError {
     print("No such file: \(error.url)")
}


So, the expression you want is, I guess, something like:


(transaction.error as? SKError).code == .paymentCancelled


with suitable optional binding to make sure it's the right kind of error.

Accepted Answer

The b4 release notes have this example:


catch let error as CocoaError where error.code == .fileReadNoSuchFileError {
     print("No such file: \(error.url)")
}


So, the expression you want is, I guess, something like:


(transaction.error as? SKError).code == .paymentCancelled


with suitable optional binding to make sure it's the right kind of error.

Thank you, that works. I don't quite follow where the "code" property of SKError is defined. Command + Click on "code" takes the IDE to the wrong place. I believe the IDE is trying to look for "Code" with an uppercase 'C', which is an enum within SKError, but "code" the property is a mystery to me -- can't find it in the SKError source or in the help documentation.

I'd guess that the "code" property is a requirement of the Error protocol that SKError must conform to.


As to visibility of the definition, it's a bridged type, so some of the details may be built into the compiler. That may be part of the answer, plus Swift 3.0 isn't quite finished yet, which may be another part of the answer.

Xcode 8 Beta 4, help with NSError / Error
 
 
Q