jh95
jh95

Reputation: 399

How can I retrieve the value from an enum?

I'm using SwiftyStoreKit and figuring out how to retrieve the status of receipt validation. I would like to retrieve status from receiptInvalid(receipt: ["status": 21004, "environment": Sandbox], status: SwiftyStoreKit.ReceiptStatus.secretNotMatching). How would I get the value 21004?

receiptInvalid is a a result of the last case:

public enum ReceiptError: Swift.Error {
// No receipt data
case noReceiptData
// No data received
case noRemoteData
// Error when encoding HTTP body into JSON
case requestBodyEncodeError(error: Swift.Error)
// Error when proceeding request
case networkError(error: Swift.Error)
// Error when decoding response
case jsonDecodeError(string: String?)
// Receive invalid - bad status returned
case receiptInvalid(receipt: ReceiptInfo, status: ReceiptStatus)
}

ReceiptInfo:

public typealias ReceiptInfo = [String: AnyObject]

End goal is to test for code 21007 for Apple App Review. Thanks!

Edit: What code goes where the comment is?

case .error(let error):
            print("Receipt verification failed: \(error)")
            //error prints receiptInvalid(receipt: ["status": 21004, "environment": Sandbox], status: SwiftyStoreKit.ReceiptStatus.secretNotMatching)
            if case .receiptInvalid = error {
                //What goes here?
            }

Upvotes: 0

Views: 264

Answers (1)

Alexander
Alexander

Reputation: 63137

You can extend enumerations with handy computed variables for accessing associated values:

 extension ReceiptError {
   var invalidReceipt: (receipt: ReceiptInfo, status: RecriptStatus)? {
       switch self {
       case .receiptInvalid(let receipt, let status):
           return (receipt: receipt, status: status)
       default:
           return nil
       }
   }
}

print(someReceiptError.invalidReceipt.receipt["status"

Upvotes: 1

Related Questions