【问题标题】:Type MyViewController does not conform to protocol 'STPPaymentContextDelegate'MyViewController 类型不符合协议“STPPaymentContextDelegate”
【发布时间】:2017-02-09 15:30:20
【问题描述】:

我正在我的类上创建一个扩展以符合协议“STPPaymentContextDelegate”。出于某种原因,编译器会抱怨,即使我已经像这样正确声明了扩展中的所有方法。

extension PaymentPopupViewController: STPPaymentContextDelegate {

func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error) {
    self.delegate?.paymentPopupViewController(controller: self, didFinishPerhapsWithError: error as NSError?)
}

func paymentContextDidChange(_ paymentContext: STPPaymentContext) {
    if paymentContext.loading {
        self.indicatorView.startAnimating()
    } else {
        self.indicatorView.stopAnimating()
        if paymentContext.selectedPaymentMethod == nil {
            self.presentPaymentMehtodsViewController()
        }
    }
    self.indicatorView.isHidden = !paymentContext.loading
    self.paymentMethodLabel.isHidden = paymentContext.loading
    self.changePaymentMethodButton.isEnabled = !paymentContext.loading
    self.payButton.isEnabled = !paymentContext.loading && paymentContext.selectedPaymentMethod != nil
    self.paymentMethodLabel.text = paymentContext.selectedPaymentMethod?.label
}

func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: STPErrorBlock) {
    fatalError("Method isn't implemented because our backend makes a charge, not the app.")

}

func paymentContext(_ paymentContext: STPPaymentContext, didFinishWith status: STPPaymentStatus, error: Error?) {
    if status == .userCancellation {
        return
    }
    self.delegate?.paymentPopupViewController(controller: self, didFinishPerhapsWithError: error as NSError?)
}

以下是协议规定的委托方法:

@protocol STPPaymentContextDelegate <NSObject>

- (void)paymentContext:(STPPaymentContext *)paymentContext didFailToLoadWithError:(NSError *)error;
- (void)paymentContextDidChange:(STPPaymentContext *)paymentContext;
- (void)paymentContext:(STPPaymentContext *)paymentContext
didCreatePaymentResult:(STPPaymentResult *)paymentResult
        completion:(STPErrorBlock)completion;
- (void)paymentContext:(STPPaymentContext *)paymentContext
didFinishWithStatus:(STPPaymentStatus)status
             error:(nullable NSError *)error;

我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c swift delegates protocols


    【解决方案1】:

    你的实现中有一些错误。

    替换这个

    func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error)
    

    func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: NSError)
    

    然后替换这个

    func paymentContext(_ paymentContext: STPPaymentContext, didFinishWith status: STPPaymentStatus, error: Error?)
    

    func paymentContext(_ paymentContext: STPPaymentContext, didFinishWithStatus status: STPPaymentStatus, error: NSError?)
    

    【讨论】:

    • 我仍然收到两个“修复”错误,希望我将 NSError 改回 Error
    • @FaisalSyed 将didFinishWith status 更改为didFinishWithStatus status 而不将错误更改为NSError 怎么样?
    • 我会尝试并告诉你
    猜你喜欢
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2019-11-23
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多