Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

objective-c - @class与#import(@class vs. #import)

It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular inclusions.

(据我了解,如果ClassA需要包括ClassB标头,而ClassB需要包括ClassA标头,以避免任何循环包含,则应使用前向类声明。)

I also understand that an #import is a simple ifndef so that an include only happens once.

(我也理解#import是一个简单的ifndef因此一个include仅发生一次。)

My inquiry is this: When does one use #import and when does one use @class ?

(我的查询是:什么时候使用#import和何时使用@class ?)

Sometimes if I use a @class declaration, I see a common compiler warning such as the following:

(有时,如果我使用@class声明, @class看到常见的编译器警告,例如:)

warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.

Would really love to understand this, versus just removing the @class forward-declaration and throwing an #import in to silence the warnings the compiler is giving me.

(真的很想了解这一点,而不是仅仅删除@class前向声明,然后抛出#import来使编译器向我发出的警告静音。)

  ask by Coocoo4Cocoa translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" ,以告诉编译器除“这是一个类”之外的其他信息。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...