Trying to add a protocol to a Class signature in swift

守給你的承諾、 提交于 2019-12-07 09:38:28

问题


I'm trying to create an in-app purchase in swift. In my class signature, I have the following:

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

However, I get an error message: type "ViewController" does not conform to protocol: SKPaymentTransactionObserver

I've read this: https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html and Conform to protocol in ViewController, in Swift

The SKSoreProductViewControllerDelegate works fine. What am I missing, please?


回答1:


Have you implemented the required methods in your class?

paymentQueue:updatedTransactions: and paymentQueue:updatedDownloads: are required methods and you will get a warning if they are not implemented.




回答2:


SKPaymentTransactionProtocol has these methods:

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]!)

The first is a required method that your class has to implement in order to conform to the protocol. Add it to your ViewController and the error will disappear.

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



回答3:


For swift use this:

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

Instead of this:

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


来源:https://stackoverflow.com/questions/24588531/trying-to-add-a-protocol-to-a-class-signature-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!