开发者

Objective c: method relation .h and .m

I have a theoretical question: but every method and IbAction m开发者_Python百科ust be declared in .h??? Because if I write a method (void) in .m and not in .h the project not has problem.


If you wanna access a function from another class you're gonna import that .h header file to make your compiler understand where to find those functions and how to translate them.


It is a guideline for how to implement your classes.

Think of it in "C" terms. You define your prototypes in the .h (header) file and do the implementation in the .c or in this case the .m file.

Both ways will work, just don't redefine it in the .m...


no, they do not all need to be declared in the header.

it is common to omit the declaration from the header when attempting to make a method 'private', by categories or extensions. whether that is more or less dangerous than declaring those in the header as private is debatable, and depends on the people using your objects.

it's also good to declare a category which is not terribly relevant to the base type in a separate header.


Yeah it's no necessary to declare method in .h that because in objective c any message can be passed to any object. That's why it doesn't give any error but only a warning "ABClass may not respond to messageABC". And for a person like me who just hate warnings declare it in .h. And this is good practice as not declaring it in header is more prone to crashes as you just ignore the warnings and the instance can't handle that message and your application would say, "Hello Mr. Crash". and "Bye-Bye" to developer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜