如何从枚举中检索值?

时间:2018-12-19 23:40:47

标签: ios swift enums swiftystorekit

我正在使用SwiftyStoreKit并弄清楚如何检索收据验证的状态。我想从status中检索receiptInvalid(receipt: ["status": 21004, "environment": Sandbox], status: SwiftyStoreKit.ReceiptStatus.secretNotMatching)。我怎么得到值 21004

receiptInvalid是最后一种情况的结果:

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]

最终目标是测试Apple App Review的代码 21007 。谢谢!

编辑:注释所在的代码是什么?

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?
            }

1 个答案:

答案 0 :(得分:1)

您可以使用方便的计算变量扩展枚举,以访问关联的值:

 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"