Objective c: method relation .h and .m

戏子无情 提交于 2019-12-13 22:46:04

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/5577489/objective-c-method-relation-h-and-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!