实现 Stripe 时必需的 Init 抛出致命错误
Required Init throwing fatal error when implementing Stripe
我正在努力将 Stripe 添加到我的项目中。还有比这更多的代码,但在我开始添加 Stripe 和 init.这是 Stripe 说要在 their docs
中使用的初始化
这是我的开始代码和初始化代码:
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. USD
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
.....
我正在使用故事板,因为我听说这很重要。当我 运行 代码时,会抛出 fatalError 并使应用程序崩溃。 The Stripe example project 里面有这个确切的代码,而且有效。
为什么我的应用程序崩溃了?这个必需的 init 甚至在做什么?我想我明白我为什么需要它,但如果你能详细说明它对子类的要求,那会很有帮助。
我的问题的解决方案是删除 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. USD
}
我留下了注释的 fatelError 部分,但如您所见,这不是必需的。就像其他人所说的那样,情节提要会使用所需的 init,并且在情节提要 class 中设置数据时必须拥有它,就像 Stripe 所要求的那样。
只要有 super.init 就可以了。
我正在努力将 Stripe 添加到我的项目中。还有比这更多的代码,但在我开始添加 Stripe 和 init.这是 Stripe 说要在 their docs
中使用的初始化这是我的开始代码和初始化代码:
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. USD
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
.....
我正在使用故事板,因为我听说这很重要。当我 运行 代码时,会抛出 fatalError 并使应用程序崩溃。 The Stripe example project 里面有这个确切的代码,而且有效。
为什么我的应用程序崩溃了?这个必需的 init 甚至在做什么?我想我明白我为什么需要它,但如果你能详细说明它对子类的要求,那会很有帮助。
我的问题的解决方案是删除 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. USD
}
我留下了注释的 fatelError 部分,但如您所见,这不是必需的。就像其他人所说的那样,情节提要会使用所需的 init,并且在情节提要 class 中设置数据时必须拥有它,就像 Stripe 所要求的那样。 只要有 super.init 就可以了。