目标 c:方法关系 .h 和 .m
我有一个理论问题:但是每个方法和 IbAction 都必须在 .h 中声明???因为如果我在 .m 中而不是在 .h 中编写方法(void),则项目不会有问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想从另一个类访问函数,您需要导入该 .h 头文件,以使编译器了解在哪里可以找到这些函数以及如何翻译它们。
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.
它是如何实施课程的指南。
用“C”术语来思考它。您可以在 .h(头文件)文件中定义原型,并在 .c 或本例中的 .m 文件中实现。
两种方法都可以,只是不要在 .m 中重新定义它...
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.
是的,没有必要在 .h 中声明方法,因为在 Objective c 中任何消息都可以传递给任何对象。这就是为什么它不会给出任何错误,而只给出警告“ABClass 可能不会响应 messageABC”。对于像我这样讨厌警告的人来说,在 .h 中声明它。这是一个很好的做法,因为不在标头中声明它更容易发生崩溃,因为您只是忽略警告并且实例无法处理该消息并且您的应用程序会说“Hello Mr. Crash”。和开发者说“再见”。
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.