当selector是静态方法时,如何设置NotificationCenter的观察者

Nik*_*ure 2 observers selector nsnotificationcenter notificationcenter swift

func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
Run Code Online (Sandbox Code Playgroud)

此函数需要观察者成为某个对象,但需要将静态方法设置为选择器.

这个答案解释了当选择器是实例方法时如何设置选择器和观察者.

Nik*_*ure 5

我们需要设置YourClass.self为观察者.通过这种方式 -

NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)
Run Code Online (Sandbox Code Playgroud)