尝试快速将协议添加到类签名中
Posted
技术标签:
【中文标题】尝试快速将协议添加到类签名中【英文标题】: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.html 和 Conform to protocol in ViewController, in Swift
SKSoreProductViewControllerDelegate 工作正常。请问我错过了什么?
【问题讨论】:
【参考方案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]!) /* */
【讨论】:
以上是关于尝试快速将协议添加到类签名中的主要内容,如果未能解决你的问题,请参考以下文章