0

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.

Mathias
  • 3,879
  • 5
  • 36
  • 48
  • 2
    No, its not compulsory to define your methods in `.h.` file unless you need to access method outside of your class. – Maulik Dec 03 '13 at 13:26

2 Answers2

0

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.

bsarr007
  • 1,914
  • 14
  • 14
0

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.

kovpas
  • 9,553
  • 6
  • 40
  • 44
  • Thanks. What makes me a bit confused, then, is all this talk in other places about private / public methods compared to java, for example here http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c -if you want the method private, isn't it, then, enough just to not have it in the header file? – Mathias Dec 03 '13 at 14:13
  • There's no such concept as "private method" in obj-c. You can send any message to any object and if object can answer to it, it will. So, if you don't see a method in .h file (which makes it "private"), but you know that this method exists, you can just do performSelector:. – kovpas Dec 03 '13 at 14:37
  • Yeah, i have figured as much, since the notificationcenter apparently can call my method :) I just meant as a definition not having the method listed in the h-file should be enough to define it as "private". I found some other threads here that kind of thought so too, so i'll stick with that i think. Thanks! – Mathias Dec 03 '13 at 15:34