If you see this warning:
(如果看到此警告:)
warning: receiver 'MyCoolClass' is a forward class and corresponding @interface may not exist
(警告:接收器“ MyCoolClass”是转发类,并且相应的@interface可能不存在)
you need to #import
the file, but you can do that in your implementation file (.m), and use the @class
declaration in your header file.
(您需要#import
文件,但是可以在实现文件(.m)中执行此操作,并在头文件中使用@class
声明。)
@class
does not (usually) remove the need to #import
files, it just moves the requirement down closer to where the information is useful.
(@class
并不会(通常)消除对#import
文件的需求,它只是将需求向下移到信息有用的地方。)
For Example
(例如)
If you say @class MyCoolClass
, the compiler knows that it may see something like:
(如果您说@class MyCoolClass
,则编译器知道它可能会显示类似以下内容:)
MyCoolClass *myObject;
It doesn't have to worry about anything other than MyCoolClass
is a valid class, and it should reserve room for a pointer to it (really, just a pointer).
(除了MyCoolClass
是有效的类之外,它不必担心其他任何事情,它应该为指向它的指针(实际上只是一个指针)保留空间。)
Thus, in your header, @class
suffices 90% of the time. (因此,在标题中, @class
足以满足90%的时间要求。)
However, if you ever need to create or access myObject
's members, you'll need to let the compiler know what those methods are.
(但是,如果您需要创建或访问myObject
的成员,则需要让编译器知道这些方法是什么。)
At this point (presumably in your implementation file), you'll need to #import "MyCoolClass.h"
, to tell the compiler additional information beyond just "this is a class". (此时(大概在您的实现文件中),您将需要#import "MyCoolClass.h"
,以告诉编译器除“这是一个类”之外的其他信息。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…