Can we not declare methods in header files?

I am watching a video course for iPad and iPhone app developers at Stanford University. The instructor says that in the video that we can control, drag the user interface object into the implementation files to create an action. But in this way, the method will not be declared in the header file. Does this mean that it is normal to implement methods in the .m file, but not to declare in the .h file?

+6
source share
3 answers

Depending on how you define "ok" :-)

Objective-C uses dynamic method searches and does not provide forced access ("private", "public", etc.) qualifiers. Therefore, you do not need to declare any method in the header file.

However, you end up fighting the compiler, as it really does type checking if you don't convince it of that and you lose it.

+6
source

You do not need to declare all implementation methods in the header file. But if you are not in the header file, you cannot refer to them by the literal name in another file and you cannot "forward the link" to the implementation file.

(Note that this is not different from regular C, but different from class methods in C ++.)

+5
source

It is OK to not declare methods in the header yes, under certain circumstances. For example, when using ARC, the compiler usually needs to know the signature of the method so that it can do the right thing. But basically all of this means that wherever you use a method, it should know about the method that you are calling.

Since you are talking about the Builder interface, this is slightly different in that it will know about all the methods, because it can “see” the whole context of your header and implementation files and know that the method exists. that is, in my terminology above, the method was defined before its use.

Regarding the definition before use, the generally accepted approach is as follows:

  • Define the method in the interface file (.h). eg:.

    Myclass.h

    @interface MyClass : NSObject - (void)someMethod; @end 

    Myclass.m

     @implementation MyClass - (void)someMethod { // do something } @end 
  • Define a method in the class continuation category. eg:.

    Myclass.h

     @interface MyClass : NSObject @end 

    Myclass.m

     @interface MyClass () - (void)someMethod; @end @implementation MyClass - (void)someMethod { // do something } @end 
+5
source

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


All Articles