【问题标题】:How can I get the description error from stripe?如何从条带中获取描述错误?
【发布时间】:2016-01-26 04:02:10
【问题描述】:

我正在我的应用程序中集成条带。代码有效,但我想得到描述错误并用标签显示给用户,以防出现问题(CC 编号不正确等):

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

【问题讨论】:

  • print(error.userInfo["NSLocalizedDescription"]) 有效吗?不确定键是否为字符串。
  • 我试过了,但它返回nil
  • 好的。我们需要知道这个对象中有什么。首先,您可以在“userInfo”上执行 ALT+CLICK,Xcode 会显示类型。也许那里有一种特定于 Stripe 的类型。如果还不够,我们将使用断点。
  • 谢谢。这是var userInfo: [NSObject : AnyObject] { get }
  • @EricD。抱歉……我虽然试过了。有用!如果你发布它,我会接受答案。欢呼

标签: swift stripe-payments


【解决方案1】:

error.userInfo 通常是一个 NSDictionary。

当你打印这个字典时,没有像 Swift 字典那样的类型,所以 print 函数并不总是能识别什么是字符串,也不会显示双引号。但是您的 userInfo 字典键仍然可能是字符串。

我会首先尝试像这样访问错误值:

print(error.userInfo["NSLocalizedDescription"])

验证NSLocalizedDescription 确实是一个字符串键。

提示:按住 ALT 并单击变量,它会显示它的类型,有助于在 Xcode 中快速调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多