非'@objc'方法'paymentAuthorizationViewControllerDidFinish'不满足'@objc'协议的要求

时间:2019-05-15 07:45:51

标签: ios swift protocol-extension

获取错误编译

  

非'@objc'方法'paymentAuthorizationViewControllerDidFinish'不满足'@objc'协议'PKPaymentAuthorizationViewControllerDelegate'的要求

如果我在PaymentAuthorizationViewControllerDidFinish之前添加@objc,那么我会得到新的错误

  

@objc只能与类的成员,@ objc协议以及类的具体扩展一起使用。

我可以直接使用PKPaymentAuthorizationViewControllerDelegate扩展Handler,但是我有两个扩展ApplePayable的不同类,并且我不想为不同的类编写相同的扩展

import UIKit
import PassKit

class ApplePayRequestComposer {}

protocol ApplePayable: PKPaymentAuthorizationViewControllerDelegate {

    func applePaymentSheet(composer: ApplePayRequestComposer) -> PKPaymentAuthorizationViewController?
    func processPKPayment(payment:PKPayment, completed:@escaping (_ success:Bool)->())
}


extension ApplePayable {

    func applePaymentSheet(composer: ApplePayRequestComposer) -> PKPaymentAuthorizationViewController?{
        return nil
    }

    func processPKPayment(payment:PKPayment, completed:@escaping (_ success:Bool)->()) {
        completed(false)
    }
}

// PKPaymentAuthorizationViewControllerDelegate implementation
extension ApplePayable
{
    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }

    @available(iOS 11.0, *)
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Swift.Void) {
        processPKPayment(payment: payment) { success in
            let status = success ? PKPaymentAuthorizationStatus.success:PKPaymentAuthorizationStatus.failure
            let result = PKPaymentAuthorizationResult(status: status, errors: nil)
            completion(result)
        }
    }

    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Swift.Void) {
        processPKPayment(payment: payment) { success in
            completion(success ? PKPaymentAuthorizationStatus.success: PKPaymentAuthorizationStatus.failure)
        }
    }
}

class Handler: NSObject, ApplePayable
{

}

1 个答案:

答案 0 :(得分:0)

尝试将@objc放在func paymentAuthorizationViewControllerDidFinish之前。

    @objc func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }