【问题标题】:Trying to add a protocol to a Class signature in swift尝试快速将协议添加到类签名中
【发布时间】:2014-07-05 16:42:24
【问题描述】:

我正在尝试快速创建应用内购买。在我的班级签名中,我有以下内容:

class ViewController: UIViewController, UITextFieldDelegate, UIAlertViewDelegate,   
SKStoreProductViewControllerDelegate, SKPaymentTransactionObserver{

但是,我收到一条错误消息:类型“ViewController”不符合协议:SKPaymentTransactionObserver

我读过这个:https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.htmlConform to protocol in ViewController, in Swift

SKSoreProductViewControllerDelegate 工作正常。请问我错过了什么?

【问题讨论】:

    标签: swift protocols storekit


    【解决方案1】:

    你是否在你的类中实现了所需的方法?

    paymentQueue:updatedTransactions:paymentQueue:updatedDownloads: 是必需的方法,如果它们没有实现,你会收到警告。

    【讨论】:

    • 我已经添加了 paymentQueue:updatedTransactions:paymentQueue:updatedDownloads: 但错误是一样的
    【解决方案2】:

    SKPaymentTransactionProtocol 有这些方法:

    func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) 
    @optional func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
    @optional func paymentQueue(queue: SKPaymentQueue!, restoreCompletedTransactionsFailedWithError error: NSError!)
    @optional func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!)
    @optional func paymentQueue(queue: SKPaymentQueue!, updatedDownloads downloads: [AnyObject]!)
    

    第一个方法是您的类必须实现以符合协议的必需方法。将其添加到您的 ViewController 中,错误就会消失。

    class ViewController: UIViewController, UITextFieldDelegate, UIAlertViewDelegate,   
    SKStoreProductViewControllerDelegate, SKPaymentTransactionObserver{
        func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!){/*...*/}
        /*...*/
    }
    

    【讨论】:

    • 在 Xcode beta 4 中应该是 func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!)
    【解决方案3】:

    为了快速使用这个:

    public func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { /* */ }
    

    而不是这个:

    public func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) { /* */ }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多