selector为静态方法时如何设置NotificationCenter的观察者
How to set NotificationCenter's observer when selector is a static method
func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
此函数需要观察者是某个对象,但同时将静态方法设置为选择器。
explains how to set selector and observer when the selector is an instance method.
我们需要设置YourClass.self
为观察者。这样-
NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)
func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
此函数需要观察者是某个对象,但同时将静态方法设置为选择器。
我们需要设置YourClass.self
为观察者。这样-
NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)