Objective c: method relation .h and .m

2019-09-21 01:16发布

I have a theoretical question: but every method and IbAction must be declared in .h??? Because if I write a method (void) in .m and not in .h the project not has problem.

4条回答
Emotional °昔
2楼-- · 2019-09-21 01:36

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...

查看更多
beautiful°
3楼-- · 2019-09-21 01:36

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.

查看更多
Melony?
4楼-- · 2019-09-21 01:37

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.

查看更多
神经病院院长
5楼-- · 2019-09-21 01:37

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.

查看更多
登录 后发表回答