我正在将 stripe 集成到我的应用程序中。代码有效,但我想得到描述错误并用标签将其显示给用户,以防出现问题(抄送号码不正确等):
// other stuff here
STPAPIClient.sharedClient().createTokenWithCard(stripeCard) { (token, error) -> Void in
if let error = error {
print(error.userInfo)
}
else if let token = token {
self.createBackendChargeWithToken(token) { status in
if status == PKPaymentAuthorizationStatus.Success{
dispatch_async(dispatch_get_main_queue()){
self.paymentActivity.hidden = true
self.paymentActivity.stopAnimating()
self.paymentActivityLabel.text = "Transaction approved!"
}
}
}
}
}
}
当我打印 error.userInfo
时,我得到以下信息:
[com.stripe.lib:ErrorMessageKey: Your card number is incorrect.,
com.stripe.lib:CardErrorCodeKey: com.stripe.lib:IncorrectNumber,
com.stripe.lib:ErrorParameterKey: number,
NSLocalizedDescription: Your card's number is invalid]
我如何推断 NSLocalizedDescription
?
最佳答案
error.userInfo
通常是一个 NSDictionary。
当你打印这个字典时,没有像 Swift 字典那样的类型,所以打印函数并不总是识别什么是字符串,也不显示双引号。但是您的 userInfo
字典键仍然可能是字符串。
我会首先尝试像这样访问错误值:
print(error.userInfo["NSLocalizedDescription"])
验证 NSLocalizedDescription
确实是一个字符串键。
提示:按住 ALT 并单击变量,它会显示它的类型,有助于在 Xcode 中快速调试。
关于swift - 如何从 Stripe 中获取描述错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000471/