Objective-C, when to declare which methods in @interface

When and what methods should be declared in the @interface section of a class? As I understand it, methods that describe what your class does should be declared in the @interface section, but other helper methods should not be declared. Is this the correct understanding on my part?

+6
source share
3 answers

Usually you should add your methods to the .h file if you want the external class to have access to it (public methods).

When they are closed (used only inside the class), just put them in your .m file.

In any case, this is just a sample. Because Objective-C works with messages, even if you do not set the method in your .h file, an external file can access it, but at least your autocomplete will not show it.

+6
source

One way is to declare an instance methods in .h file. And declare private methods inside .m using Category .

For example, in the file MyOwnClass.h .

 @interface MyOwnClass - (void)aInstanceMethod; @end 

And, inside your MyOwnClass.m file, before the @implementation block,

 @interface MyOwnClass (MyPrivateMethods) - (void)aPrivateMethod; @end 
+6
source

You should declare all your methods in your .h. EmptyStack's tip is good, but this is just a hint. If you are not going to send your binary files as an SDK, you really do not need this.

Objective-C does not have (yet) private methods.

+1
source

Source: https://habr.com/ru/post/891811/


All Articles