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

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

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

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


当前回答

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

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

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

其他回答

有关文件依赖关系& #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看作是复制-粘贴。

出于多种原因,您希望最小化导入的数量。在没有任何研究的情况下,首先想到的是它减少了编译时间。

注意,当从类继承时,不能简单地使用前向声明。您需要导入文件,以便您声明的类知道它是如何定义的。

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

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

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

如果你试图在头文件中声明一个变量,或者一个属性,你还没有导入,你会得到一个错误,说编译器不知道这个类。

你的第一个想法可能是导入它。 在某些情况下,这可能会导致问题。

例如,如果你在头文件中实现了一堆c方法,或者结构,或者类似的东西,因为它们不应该被多次导入。

因此你可以用@class告诉编译器:

我知道你不知道那门课,但它确实存在。它将被导入或在其他地方实现

它基本上告诉编译器关闭并编译,即使它不确定这个类是否会被实现。

通常在.m文件中使用#import,在.h文件中使用@class。

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

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