类型 ViewController 不符合协议 SKPaymentTransactionObserver

Posted

技术标签:

【中文标题】类型 ViewController 不符合协议 SKPaymentTransactionObserver【英文标题】:type ViewController does not conform to protocol SKPaymentTransactiionObserver 【发布时间】:2015-12-15 20:26:14 【问题描述】:

即使在输入要求代码后,我仍然会收到错误消息。我错过了什么?

class ViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate, GADBannerViewDelegate 
    @IBOutlet weak var txtYearlyIncome: UITextField!
    @IBOutlet weak var sldPercentSaved: UISlider!
    @IBOutlet weak var lblPercent: UILabel!
    @IBOutlet weak var lblWeeklySaved: UILabel!
    @IBOutlet weak var lblMonthlySaved: UILabel!
    @IBOutlet weak var lblYearlySaved: UILabel!
    @IBOutlet weak var lblSaved25: UILabel!
    @IBOutlet weak var btnRemoveAds: UIButton!
    @IBOutlet weak var bannerView: GADBannerView!
    @IBOutlet weak var lblAdsRemoved: UILabel!

    var yearlyIncomeString = ""
    var yearlyIncomeFloat : Float = 0.0
    var percent : Float = 0.0
    var weeklySaved : Float = 0.0
    var MonthlySaved : Float = 0.0
    var YearlySaved : Float = 0.0
    var Saved25 : Float = 0.0
    var decimalValue : Float = 0.0
    var product: SKProduct!

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.bannerView.adUnitID = "ca-app-pub-3042973809533220/1135061595"
        self.bannerView.rootViewController = self

        let request : GADRequest = GADRequest ()

        self.bannerView.loadRequest (request)
        self.bannerView.hidden = true
        self.bannerView.delegate = self
        btnRemoveAds.hidden = true
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) 
        let products = response.products
        if (products.count != 0)
        
            product = products[0] as SKProduct
        

        btnRemoveAds.hidden = true

        if (NSUserDefaults.standardUserDefaults().objectForKey("Ads") == nil)
        
            SKPaymentQueue.defaultQueue().addTransactionObserver(self)
            self.getProductInfo();
        

        if let status: AnyObject! = NSUserDefaults.standardUserDefaults().objectForKey("Ads")
            if status as! NSString == "purchased" 
                self.bannerView.removeFromSuperview()
                self.btnRemoveAds.hidden = true;
                self.btnRemoveAds.userInteractionEnabled = false
            
            else 
                SKPaymentQueue.defaultQueue().addTransactionObserver(self)
                self.getProductInfo()
            
        
    

    func getProductInfo()
        if SKPaymentQueue.canMakePayments()
            let productID:NSSet = NSSet(object:"smmremoveads")
            let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
            request.delegate = self
            request.start()
        

        func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
            for transaction:AnyObject in transactions 
                if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction
                    switch trans.transactionState 
                    case .Purchased:
                        SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                        storePurchase()
                        lblAdsRemoved.text = "Ads Removed!"
                        break
                    case .Failed:
                        SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                        break
                    case .Restored:
                        SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                        break
                    default:
                        break
                    
                
            
        

        func storePurchase () 
            NSUserDefaults.standardUserDefaults().setObject("purchased", forKey: "Ads")
            self.bannerView.removeFromSuperview()
            self.btnRemoveAds.hidden = true
            self.btnRemoveAds.enabled = false
            lblAdsRemoved.text = "Ads Removed"
        
    

    @IBAction func sldPercentSavedACTION(sender: UISlider) 
        takeINData()
        calculate()
    

    @IBAction func btnRemoveAdsACTION(sender: UIButton) 
    

    @IBAction func btnClearACTION(sender: UIButton) 
        clear()
    

    func takeINData () 
        if txtYearlyIncome.text == ""
            txtYearlyIncome.text = "10000"
        

        yearlyIncomeString = txtYearlyIncome.text!
        yearlyIncomeFloat = Float(yearlyIncomeString)!
        percent = sldPercentSaved.value
    

    func calculate () 
        YearlySaved = yearlyIncomeFloat * percent
        MonthlySaved = YearlySaved / 12
        weeklySaved = YearlySaved / 52
        Saved25 = YearlySaved * 25
        print()
    

    func print () 
        percent = percent * 100

        let sPercent = String(format: "%0.0f", percent)
        let sWeekly = String(format: "%0.0f", weeklySaved)
        let sMonthly = String(format: "%0.0f", MonthlySaved)
        let sYearly = String(format: "%0.0f", YearlySaved)
        let sSaved25 = String(format: "%0.0f", Saved25)

        lblPercent.text! = "\(sPercent)%"
        lblWeeklySaved.text = "$\(sWeekly)"
        lblMonthlySaved.text = "$\(sMonthly)"
        lblYearlySaved.text! = "$\(sYearly)"
        lblSaved25.text = "$\(sSaved25)"
    

    func clear () 
        txtYearlyIncome.text! = ""
        lblWeeklySaved.text = "$"
        lblMonthlySaved.text = "$"
        lblYearlySaved.text! = "$"
        lblSaved25.text = "$"

        lblPercent.text! = "0%"
    

    func hideKeyboard () 
        txtYearlyIncome.resignFirstResponder()
    

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
      hideKeyboard()
    

    func round () 

    

【问题讨论】:

忘了开头类 ViewController:UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate, GADBannerViewDelegate 【参考方案1】:

在我看来,您缺少右括号,但很难说,因为您的代码格式不正确(我稍后会为您解决这个问题!)

问题出在这里:

func getProductInfo() 
    if SKPaymentQueue.canMakePayments() 
        let productID:NSSet = NSSet(object:"smmremoveads")
        let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
        request.delegate = self
        request.start()
       

    func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
        for transaction:AnyObject in transactions 

注意getProductInfo 如何打开两个大括号 () 并只关闭一个?这就是问题所在:func paymentQueue 之前需要第二个 。

【讨论】:

感谢 TwoStraws 的工作,但它也导致了一系列未解决的标识符错误,从 if txtYearlyIncome.text == "" on down ??? 猜测可能是因为storePurchase() 后面多了一个大括号。如果您的错误类似于“只有实例方法可以声明为 @IBAction`,然后尝试在 sldPercentSavedACTION() 之前删除一个大括号。 所以经验法则是我得到这些类型的错误,我应该检查是否丢失或删除大括号。我之所以问,是因为我试图在另一个应用程序上添加 IAP,并且我得到相同类型的 ViewController 不符合协议 SKPaymentTransactionObserver 超时我 addclass ViewController: UIViewController, SKPaymentTransactionObserver,SKProductsRequestDelegate, GADBannerViewDelegate 经验法则:如果您从某个时间点开始到处都出现错误,那么您几乎可以肯定缺少大括号或引号 :) 如果您还有其他问题,您应该将它们作为新问题发布;在 cmets 中发布代码不是一个好主意。如果我的回答解决了您的问题,您能否将其标记为正确,以便其他人也可以受益?谢谢! 非常感谢 TwoStraws 你太棒了!

以上是关于类型 ViewController 不符合协议 SKPaymentTransactionObserver的主要内容,如果未能解决你的问题,请参考以下文章

类型“ViewController”不符合协议“UITableViewDataSource”

类型 ViewController 不符合协议 SKPaymentTransactionObserver

Swift UITableView 控制器错误:类型“ViewController”不符合协议“UITableViewDataSource”

ViewController 不符合协议 UITableViewDataSource

多种协议的 Swift 类一致性(XCode 7、iOS 9、Swift 2.1)

ViewController 不符合自定义 UIButton 类