如何从枚举中检索值?
How can I retrieve the value from an enum?
我正在使用 SwiftyStoreKit
并弄清楚如何检索收据验证的状态。我想从 receiptInvalid(receipt: ["status": 21004, "environment": Sandbox], status: SwiftyStoreKit.ReceiptStatus.secretNotMatching)
检索 status
。我如何获得值 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)
}
收据信息:
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?
}
您可以使用方便的计算变量扩展枚举以访问关联值:
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"
我正在使用 SwiftyStoreKit
并弄清楚如何检索收据验证的状态。我想从 receiptInvalid(receipt: ["status": 21004, "environment": Sandbox], status: SwiftyStoreKit.ReceiptStatus.secretNotMatching)
检索 status
。我如何获得值 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)
}
收据信息:
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?
}
您可以使用方便的计算变量扩展枚举以访问关联值:
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"