【问题标题】:getting initialization error when trying to segue or instantiate to vc尝试 segue 或实例化到 vc 时出现初始化错误
【发布时间】:2021-06-17 00:19:37
【问题描述】:

所以我的目标是将数据传输到 Stripe 为其 API 提供的CheckoutViewController()。所以像往常一样,我去了故事板并将一个新的 vc 与 CheckoutViewController() 类连接起来,这样我就可以从我的另一个 vc 中转到它并进行数据传输。

在此之前,我只是在推送 vc,它工作正常,并且知道我可以实例化它并进行数据传输,但即使我尝试这样做,我也会收到一个致命错误:

Fatal error: init(coder:) has not been implemented

这是我在CheckoutVC 中的代码:

let paymentContext: STPPaymentContext?
let config: STPPaymentConfiguration

let customerContext = STPCustomerContext(keyProvider: MyAPIClient())

init() {
    let config = STPPaymentConfiguration()
    let paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: .defaultTheme)
    config.requiredBillingAddressFields = .name
    self.paymentContext = paymentContext
    self.config = config
    super.init(nibName: nil, bundle: nil)
    self.paymentContext?.delegate = self
    self.paymentContext?.hostViewController = self
}

required init?(coder aDecoder: NSCoder) {
   super.init(coder: aDecoder)
}

我更改了required init,因为我阅读了this post on init errors,但现在它甚至在运行之前就给出了错误:

Property 'self.paymentContext' not initialized at super.init call

在我将课程连接到情节提要中的 vc 之前,这一切都有效,但我确实别无选择,我需要为用户进行数据传输以支付适当的金额。我不明白self.paymentContext 是如何没有初始化的,我以前用过这个就好了。有人知道我该如何解决或解决它吗?

init() {
   
    super.init(nibName: nil, bundle: nil)
    
}

required init?(coder aDecoder: NSCoder) {
    let config = STPPaymentConfiguration()
    let paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: .defaultTheme)
    config.requiredBillingAddressFields = .name
    self.paymentContext = paymentContext
    self.config = config
    self.paymentContext?.delegate = self
    self.paymentContext?.hostViewController = self
   super.init(coder: aDecoder)
}

像这样?

【问题讨论】:

    标签: ios swift initialization stripe-payments nscoder


    【解决方案1】:

    当出现故事板时,调用的是 initWithCoder:,而不是您的 init() 函数,并且您的 initWithCoder 实现在初始化所有属性之前调用 super

    创建一个名为 commonInit 的函数并让所有不同的 initWith..s 调用 commonInit 可能会有所帮助,因此您的视图控制器可以通过编程方式或从 nib 等实例化

    【讨论】:

    • 所以不是 init() 本身做 init(with coder: NSCoder)?你能添加一个代码sn-p吗? @Shadowrun
    • 我正在浏览所有这些关于 initWithCoder 的 SO 帖子,它们都来自 2013 年,它是 Objective-C 代码,我还能在 Swift 5 中使用它吗? @Shadowrun
    • 只需要从你的 init 中取出 super.init 调用之外的所有东西,然后在 super.initWithCoder 调用之前将它放入你的 init 中
    • 检查我所做的编辑,看看是否正确。 @Shadowrun
    【解决方案2】:

    上面的哥们解释的方式真的让我很吃惊,但最终我意识到他基本上是对的,我需要做的就是:

    let paymentContext: STPPaymentContext
    let config: STPPaymentConfiguration
    
    let customerContext = STPCustomerContext(keyProvider: MyAPIClient())
    
    required init?(coder aDecoder: NSCoder) {
        let config = STPPaymentConfiguration()
        let paymentContext = STPPaymentContext(customerContext: customerContext, configuration: config, theme: .defaultTheme)
        config.requiredBillingAddressFields = .name
        self.paymentContext = paymentContext
        self.config = config
        super.init(coder: aDecoder )
        self.paymentContext.delegate = self
        self.paymentContext.hostViewController = self
    }
    

    现在效果很好。

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2023-03-20
      • 2021-09-27
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多