简体   繁体   English

Swift中的通知观察器错误

[英]Notification Observer Error In Swift

I'm using this IAP implementation guide and when a purchase is final, it posts a notification using this code: 我正在使用此IAP实施指南 ,当最终购买时,它将使用以下代码发布通知:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier)

The implementation doesn't include observer code, so I added the below into the function that runs when the "Buy" button is pressed: 该实现不包含观察者代码,因此我在按下“购买”按钮时运行的函数中添加了以下内容:

var functionToRun = #selector(MyViewController.runAfterBoughtFunc)
NotificationCenter.default.addObserver(self, selector: functionToRun, name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object:nil)

My problem is that when the NotificationCenter.default.post(...) code gets called I get this error: 我的问题是,当NotificationCenter.default.post(...)代码被调用时,出现此错误:

...runAfterBoughtFuncWithNotif:]: unrecognized selector sent to instance 0x100f12fc0
(lldb) 

Notes 笔记

  1. If I comment-out the observer code, I no longer get an error. 如果我注释掉观察者代码,则不会再出现错误。
  2. If you look at the IAP Guide I'm using, in the comments another user had the same problem, so this is not isolated to only my code 如果您查看我正在使用的《 IAP指南》,则在注释中其他用户也遇到相同的问题,因此这不仅限于我的代码

Anyone know how to fix it? 谁知道怎么修它?

Because no answers were posted, I had to come up with a work around. 由于未发布任何答案,因此我不得不提出解决方案。 So if you're having trouble with using the observer pattern, you can try this alternative. 因此,如果您在使用观察者模式时遇到麻烦,可以尝试这种替代方法。

My Custom Observer 我的自定义观察者

The below code has a variable to hold the status of the thing being observed. 以下代码具有一个变量,用于保存被观察事物的状态。 It then uses a timer to periodically run a function that checks that variable to see if the state changed. 然后,它使用计时器来定期运行一个函数,该函数检查该变量以查看状态是否已更改。 The below example is of observing a purchase status. 以下示例说明了购买状态。

  1. Set a variable that points at our custom observer, mine is checkPurchase status. 设置一个指向我们的自定义观察者的变量,我的是checkPurchase status。 You'll most likely want to add this to the button-push code or viewDidLoad, whatever sets things off. 无论什么设置,您很可能希望将其添加到按钮推送代码或viewDidLoad中。

     let selector = #selector(myViewController.checkPurchaseStatus) 
  2. Then set a timer that periodically runs that runs the function, in my case it will run "checkPurchaseStatus" once each second. 然后设置一个定期运行的计时器来运行该功能,在我的情况下,它将每秒运行一次“ checkPurchaseStatus”。 Add this under the above code. 在上面的代码下添加它。

     timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: selector, userInfo: nil, repeats: true) 
  3. Then set a variable that keeps track of the "state". 然后设置一个跟踪“状态”的变量。 If the state will be determined by another ViewController, you can make this a global variable: 如果状态将由另一个ViewController确定,则可以将其设置为全局变量:

     var purchaseState = "" 
  4. List item 项目清单

     func checkPurchaseStatus () { switch purchaseState { case "": print("Waiting for purchase...") case "failed": timer.invalidate() //turns timer off purchaseState = "" //resets the variable for future uses //Add more code to be run if the purchase failed print("Purchased failed. Timer stopped.") case "succeeded": timer.invalidate() //turns timer off purchaseState = "" //resets the variable for future uses //Add more code to be run if the purchase succeeded print("Purchased succeeded. Timer stopped.") default: break } } 

And that's it! 就是这样! It's not as powerful as an observer but it's a simple, quick way to get the job done in most situations! 它不像观察员那么强大,但是它是在大多数情况下完成工作的简单,快捷的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM