Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

This function needs the observer to be some object but while setting static methods as the selector.

This answer explains how to set selector and observer when the selector is an instance method.

like image 204
Nikhil Manapure Avatar asked Feb 06 '23 05:02

Nikhil Manapure


1 Answers

We need set YourClass.self as the observer. In this way -

NotificationCenter.default.addObserver(YourClass.self, selector: #selector(YourClass.yourStaticMethod), name: NSNotification.Name.BlahBlah, object: nil)
like image 154
Nikhil Manapure Avatar answered Feb 07 '23 17:02

Nikhil Manapure