根据我的理解,在ClassA需要包含ClassB标头,而ClassB需要包含ClassA标头以避免任何循环包含的情况下,应该使用前向类声明。我还理解#import是一个简单的ifndef,因此include只发生一次。

我的问题是:什么时候使用#import,什么时候使用@class?有时如果我使用@class声明,我看到一个常见的编译器警告,如下所示:

警告:接收端FooController是转发类,对应的@interface可能不存在。

我真的很想理解这一点,而不是仅仅删除@class forward-declaration并抛出一个#import来沉默编译器给我的警告。


当前回答

有关文件依赖关系& #import & @class的更多信息,请查看以下内容:

http://qualitycoding.org/file-dependencies/ 这是篇好文章

文章摘要

imports in header files: #import the superclass you’re inheriting, and the protocols you’re implementing. Forward-declare everything else (unless it comes from a framework with a master header). Try to eliminate all other #imports. Declare protocols in their own headers to reduce dependencies. Too many forward declarations? You have a Large Class. imports in implementation files: Eliminate cruft #imports that aren’t used. If a method delegates to another object and returns what it gets back, try to forward-declare that object instead of #importing it. If including a module forces you to include level after level of successive dependencies, you may have a set of classes that wants to become a library. Build it as a separate library with a master header, so everything can be brought in as a single prebuilt chunk. Too many #imports? You have a Large Class.

其他回答

我看到很多“这样做”,但我没有看到任何“为什么”的答案。

那么:为什么在头文件中使用@class,而在实现中使用#import呢?一直使用@class和#import会使你的工作加倍。除非你使用继承。在这种情况下,您将为单个@class #import多次。然后,如果您突然决定不再需要访问某个声明,您必须记得从多个不同的文件中删除。

由于#import的性质,多次导入同一个文件不是问题。 编译性能也不是真正的问题。如果是的话,我们就不会在几乎每个头文件中都使用#importing Cocoa/Cocoa.h之类的代码了。

向前声明只是为了防止编译器显示错误。

编译器会知道你在头文件中声明的类的名称。

如果你看到这个警告:

警告:接收端MyCoolClass是转发类,对应的@interface可能不存在

您需要#导入文件,但是您可以在实现文件(.m)中这样做,并在头文件中使用@class声明。

@class并不(通常)消除#import文件的需要,它只是把需求移到更接近信息有用的地方。

例如

如果你说@class MyCoolClass,编译器知道它可能会看到类似这样的东西:

MyCoolClass *myObject;

除了MyCoolClass是一个有效的类之外,它不需要担心其他任何事情,并且它应该为指向它的指针保留空间(实际上,只是一个指针)。因此,在头文件中,@class占据了90%的时间。

然而,如果你需要创建或访问myObject的成员,你需要让编译器知道这些方法是什么。在这一点上(大概在你的实现文件中),你需要#import "MyCoolClass.h",告诉编译器除了"这是一个类"之外的其他信息。

常见的做法是在头文件中使用@class(但仍然需要#import父类),在实现文件中使用#import。这样可以避免任何圆形夹杂物,而且效果很好。

当我发展的时候,我脑中只有三件事,这三件事从来不会给我带来任何问题。

导入超类 导入父类(当您有子类和父类时) 在项目外部导入类(比如在框架和库中)

对于所有其他类(我的项目中的子类和子类),我通过forward-class声明它们。