简体   繁体   English

Objective-C @类/导入最佳实践

[英]Objective-C @class / import best practice

I've noticed that a lot of Objective-C examples will forward declare classes with @class, then actually import the class in the .m file with an import. 我注意到,很多Objective-C示例都会使用@class转发声明的类,然后通过导入将类实际导入到.m文件中。 I understand that this is considered a best practice, as explained in answers to question: @class vs. #import 我了解这被认为是最佳做法,如以下问题的答案所述: @class vs. #import

Coming from C++ this feels backwards. 来自C ++,这让人感到倒退。 I would normally include all needed .h files in the new classes header file. 我通常会在新的类头文件中包含所有需要的.h文件。 This seems useful since it would make the compiler generate a warning when two classes include each other, at which point I can decide whether this is a bad thing or not then use the same Objective-C style and forward declare the class in the header and include it in the .cpp file. 这似乎很有用,因为当两个类相互包含时,它会使编译器生成警告,此时,我可以判断这是否不好,然后使用相同的Objective-C样式并在标头中声明该类,然后将其包括在.cpp文件中。

What is the benefit of forward declaring @class and importing in the implementation file? 向前声明@class并导入到实现文件中有什么好处? Should it be a best practice in C++ to forward declare classes rather than including the header file? 在C ++中,最好的做法是转发声明的类而不是包含头文件? Or is it wrong to think of Objective-C and C++ in these similar terms to begin with? 还是以这些相似的术语来考虑Objective-C和C ++是错误的?

To be honest, your C++ is actually backwards. 老实说,您的C ++实际上倒退了。 Generally in C++ you want to avoid headers being included inside other headers preferring forward declarations to includes. 通常,在C ++中,您要避免将标头包含在其他标头中,而更喜欢将前向声明包括在内。 This is generally considered the best practice because it decreases compile time, and shrinks the size of the preprocessed code files fed into the compiler to as small as needed. 通常认为这是最佳做法,因为它可以减少编译时间,并将馈入编译器中的预处理代码文件的大小缩小到所需的大小。 Scott Meyers has a great section about that in Effective C++. 斯科特·迈耶斯(Scott Meyers)在有效的C ++中对此进行了精彩介绍。

To more directly answer your question, the advantage of forward declaring classes, and importing them in the implementation file (in both C++ and objective c), is basically that forward declarations make it so that any other class using your class doesn't necessarily need to include all of the things your class uses. 为了更直接地回答您的问题,前向声明类并将其导入到实现文件中(在C ++和目标c中)的优点是,基本上,前向声明使之成为可能,因此使用您的类的任何其他类都不一定需要包括您的课堂使用的所有东西。 Which reduces the size of preprocessed code files (which makes preprocessing faster), makes compilation faster, and linking faster. 这减少了预处理代码文件的大小(这使预处理速度更快),使编译速度更快,并且链接速度更快。 All of which are usually good things. 所有这些通常都是好东西。 In more obscure cases having reduced include statements can make it easier to find certain types of errors (like missing semicolons in headers) that produce compiler warnings that aren't always obvious but are repeated and spammed everywhere headers are included. 在更晦涩的情况下,减少include语句可以使查找某些类型的错误(例如,标头中缺少分号)更容易,这些错误会产生并不总是很明显的编译器警告,但在包含标头的所有地方都会重复出现并发送垃圾邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM