如何解析从条带接收到的nserror

how to parse nserror received from stripe

您好,当我输入一些不正确的卡号时出现此类型错误

Error Domain=com.stripe.lib Code=70 "Your card was declined" UserInfo={com.stripe.lib:ErrorMessageKey=Your card was declined. Your request was in live mode, but used a known test card., com.stripe.lib:StripeErrorCodeKey=card_declined, com.stripe.lib:StripeErrorTypeKey=card_error, com.stripe.lib:CardErrorCodeKey=com.stripe.lib:CardDeclined, com.stripe.lib:ErrorParameterKey=, NSLocalizedDescription=Your card was declined}

我想从以下错误中解析此消息

Your card was declined. Your request was in live mode, but used a known test card.

您需要将 error 转换为 NSError 然后您可以访问 userInfo 并检查 dictionary 中的所有 keys 并提取无论你想要什么消息值,

if let error = error as? NSError {
    print(error.userInfo)
    print(error.localizedDescription)
    // Check the userInfo object and extract the messages as error.userInfo["messageKey"]
}