Swift 如何设置通知中心&x27;当选择器为静态方法时,s观察者

Swift 如何设置通知中心&x27;当选择器为静态方法时,s观察者,swift,selector,nsnotificationcenter,observers,notificationcenter,Swift,Selector,Nsnotificationcenter,Observers,Notificationcenter,此函数需要观察者是某个对象,但将静态方法设置为选择器时 解释当选择器是实例方法时如何设置选择器和观察者我们需要将YourClass.self设置为观察者。这样- func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?) NotificationCenter.default.addObserver(YourClas

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


解释当选择器是实例方法时如何设置选择器和观察者

我们需要将
YourClass.self
设置为观察者。这样-

func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)
NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)