Should Notificationcenter observer selector methods go in the header file?

183 views Asked by At

One of the things i (still) have trouble getting into the back of my head is public/private methods, parameters and how it works compared to the Java world i have been living in for a long time.

I have started using the notificationcenter since a while back, and i noticed that you can declare a method within your implementation file (.m) and use that as a selector when registering your observer in the nsnotificationcenter

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aMethodNotDefinedInTheHeaderFile:) name:NOTIFICATION_KEY object:nil];

So, my question is - what is best practice here? Should i still define the method in my header file, and to what end?

Pointers much appreciated.

2

There are 2 answers

3
kovpas On BEST ANSWER

Declaring methods in header files is used to get users of the class overview of a methods which they can use.

As soon as subscribing for a notification is usually an internal thing (you subscribe and receive notification in the same class), I'd say it's not needed.

However, if the method is a part of API, which can be used elsewhere, you'd probably want to put it in .h file.

0
bsarr007 On

You don't need to define your method in the header file since you don't need to access this method outside your class, your method will be only used by the Notification Center. Since you defined the method inside your implementation file, you will respond the the selector. This is OK.