0

I am working on adding Stripe into my project. There's more code than this, but it was all working before I began adding in Stripe and the init. This is the init that Stripe says to use in their docs

Here's my beginning code and init code:

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")
}
.....

I am using Storyboards, as I've heard that matters. When I run the code, the fatalError gets thrown and crashes the app. The Stripe example project has this exact code in there, and it works.

Why is my app crashing? What is this required init even doing? I think I understand why I need it, but if you could elaborate beyond it's required for subclasses, that would be helpful.

gcharita
  • 6,251
  • 3
  • 16
  • 29
ATCraiger
  • 69
  • 1
  • 9
  • Does this answer your question? [Fatal error: use of unimplemented initializer 'init(coder:)' for class](https://stackoverflow.com/questions/24036393/fatal-error-use-of-unimplemented-initializer-initcoder-for-class) – karllekko Dec 02 '20 at 09:35
  • It's required when using Storyboards(the Stripe example app doesn't, so that constructor never actually gets called) because the UIStoryboard will use that constructor to instantiate your BusinessOwnerVC. – karllekko Dec 02 '20 at 09:37

1 Answers1

0

The solution to my issue was removing the init and only user the required init, like so:

 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
}

I left the commented fatelError portion, but as you can see it's not necessary. It's like the others said, the required init gets used by storyboards and you must have it when you're setting up data in your storyboard class, like Stripe requires. Just have the super.init in there and you should be all good to go.

ATCraiger
  • 69
  • 1
  • 9