Nikhil Manapure
Nikhil Manapure

Reputation: 3878

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.

Upvotes: 4

Views: 1380

Answers (1)

Nikhil Manapure
Nikhil Manapure

Reputation: 3878

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)

Upvotes: 9

Related Questions