【问题标题】:Required Init throwing fatal error when implementing Stripe实现 Stripe 时必需的 Init 抛出致命错误
【发布时间】:2021-03-14 01:49:47
【问题描述】:

我正在努力将 Stripe 添加到我的项目中。还有比这更多的代码,但在我开始添加 Stripe 和 init 之前,这一切都在工作。这是 Stripe 说要在 their docs 中使用的 init

这是我的开始代码和初始化代码:

class BusinessOwnerVC: UIViewController, MyProtocol {

let paymentContext: STPPaymentContext


init() {
    let customerContext = STPCustomerContext(keyProvider: SwiftAPI())
    self.paymentContext = STPPaymentContext(customerContext: customerContext)
    super.init(nibName: nil, bundle: nil)
    self.paymentContext.delegate = self
    self.paymentContext.hostViewController = self
    self.paymentContext.paymentAmount = 5000 // This is in cents, i.e. $50 USD
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
.....

我正在使用 Storyboard,因为我听说这很重要。当我运行代码时,会抛出 fatalError 并使应用程序崩溃。 The Stripe example project 里面有这个确切的代码,它可以工作。

为什么我的应用程序崩溃了?这所需的 init 甚至在做什么?我想我明白我为什么需要它,但如果您能在子类所需之外进行详细说明,那将很有帮助。

【问题讨论】:

标签: ios swift sdk stripe-payments


【解决方案1】:

我的问题的解决方案是删除 init 并且只使用所需的 init,如下所示:

 required init?(coder aDecoder: NSCoder) {
    //fatalError("init(coder:) has not been implemented")
    let customerContext = STPCustomerContext(keyProvider: SwiftAPI())
    self.paymentContext = STPPaymentContext(customerContext: customerContext)
    super.init(coder: aDecoder)
    self.paymentContext.delegate = self
    self.paymentContext.hostViewController = self
    self.paymentContext.paymentAmount = 5000 // This is in cents, i.e. $50 USD
}

我留下了注释的 fatelError 部分,但正如您所见,这不是必需的。就像其他人所说的那样,情节提要使用所需的 init ,当您在情节提要类中设置数据时,您必须拥有它,就像 Stripe 需要的那样。 只需将 super.init 放入其中,一切顺利。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 2019-09-17
    • 2022-01-23
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多