Declaring methods in .h file
if i want to create a helper metho开发者_开发问答d in my .m file. its call it -(void) helpMeDoSomething... etc. do i need to declare the function prototype in the .h file like in c/c++ or just declaring it in the .m file is enough
Neither C, C++, nor Objective-C require function declarations to be in the header file. They simply have to be declared before they are used, and the definition in the .m file can serve as the declaration.
In order for other classes to see the method, its signature must be in the header file. If you are using the method in the same class that it is defined in, it does not need to be in the header file.
Put the prototype in the .h file if you want to make it available to be called from code in other files. You can put it in the .m file if it will only be called from inside that one file.
As in C/C++, you can declare it in your .m
file as long as you declare it before you use it, and as long as you don't need it somewhere else.
精彩评论