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

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

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

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


当前回答

我的问题是这样的。什么时候使用#import,什么时候使用@class?

简单的回答:当存在物理依赖时,使用#import或#include。否则,使用正向声明(@class MONClass, struct MONStruct, @protocol MONProtocol)。

以下是一些常见的身体依赖的例子:

任何C或c++值(指针或引用不是物理依赖项)。如果您将CGPoint作为ivar或属性,编译器将需要看到CGPoint的声明。 你的超类。 你使用的方法。

有时如果我使用@class声明,我看到一个常见的编译器警告,如下所示: “警告:接收端‘FooController’是转发类,对应的@interface可能不存在。”

The compiler's actually very lenient in this regard. It will drop hints (such as the one above), but you can trash your stack easily if you ignore them and don't #import properly. Although it should (IMO), the compiler does not enforce this. In ARC, the compiler is more strict because it is responsible for reference counting. What happens is the compiler falls back on a default when it encounters an unknown method which you call. Every return value and parameter is assumed to be id. Thus, you ought to eradicate every warning from your codebases because this should be considered physical dependence. This is analogous to calling a C function which is not declared. With C, parameters are assumed to be int.

The reason you would favor forward declarations is that you can reduce your build times by factors because there is minimal dependence. With forward declarations, the compiler sees there is a name, and can correctly parse and compile the program without seeing the class declaration or all of its dependencies when there is no physical dependency. Clean builds take less time. Incremental builds take less time. Sure, you will end up spending a little more time making sure the all the headers you need are visible to every translation as a consequence, but this pays off in reduced build times quickly (assuming your project is not tiny).

如果您使用#import或#include来代替,则会向编译器抛出大量不必要的工作。您还引入了复杂的头依赖项。你可以把它比作一个蛮力算法。当您使用#import时,会拖入大量不必要的信息,这需要大量内存、磁盘I/O和CPU来解析和编译源代码。

ObjC is pretty close to ideal for a C based language with regards to dependency because NSObject types are never values -- NSObject types are always reference counted pointers. So you can get away with incredibly fast compile times if you structure your program's dependencies appropriately and forward where possible because there is very little physical dependence required. You can also declare properties in the class extensions to further minimize dependence. That's a huge bonus for large systems -- you would know the difference it makes if you have ever developed a large C++ codebase.

因此,我的建议是尽可能使用forward,然后在有物理依赖的地方使用#import。如果您看到警告或其他暗示身体依赖的警告,请全部修复。修复方法是在实现文件中使用#import。

在构建库时,您可能会将一些接口分类为一组,在这种情况下,您将#导入引入物理依赖的库(例如#import <AppKit/AppKit.h>)。这可能会引入依赖关系,但是库维护者通常可以根据需要为您处理物理依赖关系——如果他们引入了一个特性,他们可以将其对构建的影响最小化。

其他回答

如果我们这么做

@interface Class_B : Class_A

意味着我们将Class_A继承到Class_B,在Class_B中我们可以访问Class_A的所有变量。

如果我们这样做

#import ....
@class Class_A
@interface Class_B

这里我们说我们在程序中使用Class_A,但如果我们想在Class_B中使用Class_A变量,我们必须在.m文件中#import Class_A(创建一个对象并使用它的函数和变量)。

可以把@class看作是告诉编译器“相信我,这是存在的”。

可以把#import看作是复制-粘贴。

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

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

编译器只有在编译器需要知道其实现的情况下才会报错。

Ex:

这可以是,如果你要从中派生你的类或者 如果你要将该类的一个对象作为成员变量(尽管很少)。

如果你只是把它用作指针,它不会抱怨。当然,您必须在实现文件中#import它(如果您正在实例化该类的对象),因为它需要知道实例化对象的类内容。

注意:#import和#include不一样。这意味着没有所谓的循环导入。Import是一种请求,要求编译器查看特定文件以获取某些信息。如果该信息已经可用,编译器将忽略它。

试试这个,在B.h中导入A.h,在A.h中导入B.h。不会有任何问题或抱怨,它也会正常工作。

何时使用@class

只有当你不想在你的头文件中导入头文件时,你才可以使用@class。在这种情况下,你甚至不关心这个类是什么。在这种情况下,您甚至可能还没有该类的头文件。

例如,您正在编写两个库。一个类,我们称之为A,存在于一个库中。该库包含来自第二个库的标头。该头文件可能有一个指针a,但同样可能不需要使用它。如果库1还不可用,如果使用@class,库B将不会被阻塞。但是如果您想导入A.h,那么库2的进程就会被阻塞。

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

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

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

如果需要,在头文件中使用前向声明,并为在实现中使用的任何类导入头文件。换句话说,您总是#import您在实现中使用的文件,如果您需要在头文件中引用一个类,也可以使用forward声明。

例外的是,你应该在头文件中导入一个类或正式协议(在这种情况下,你不需要在实现中导入它)。