xcode #import 和@class 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19465059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between #import and @class
提问by Gustl007
i recently watched a tutorial where the speaker said that it makes no difference if you use:
我最近观看了一个教程,其中演讲者说如果您使用以下内容没有任何区别:
#import "Class.h"
or:
或者:
@class Class;
And i have to say, my apps work the one way or the other. But there have to be a difference right? So thought i ask you guys.
我不得不说,我的应用程序以一种或另一种方式工作。但一定有区别吧?所以想我问你们。
Thanks in advance!
提前致谢!
回答by Vineet Singh
"#import"brings the entire header file in question into the current file; any files that THAT file #importsare also included.
“#import”将整个头文件带入当前文件;还包括该文件#imports 的任何文件。
@class, on the other hand (when used on a line by itself with some class names), just tells the compiler "Hey, you're going to see a new token soon; it's a class, so treat it that way).
@class,另一方面(当在一行中使用一些类名时),它只是告诉编译器“嘿,你很快就会看到一个新的标记;它是一个类,所以这样对待它)。
This is very useful when you've got the potential for 'circular includes'; ie, Object1.h makes reference to Object2, and Object2.h makes reference to Object1. If you #import both
files into the other, the compiler can get confused as it tries to #import
Object1.h, looks in it and sees Object2.h; it tries to #import
Object2.h, and sees Object1.h, etc.
当您有“循环包含”的潜力时,这非常有用;即,Object1.h 引用Object2,Object2.h 引用Object1。如果您将#import both
文件写入另一个,编译器会在尝试使用#import
Object1.h、查看它并看到 Object2.h 时感到困惑;它尝试#import
Object2.h,并看到 Object1.h 等。
If, on the other hand, each of those files has @class
Object1; or @class
Object2;, then there's no circular reference. Just be sure to actually #import
the required headers into your implementation (.m) files.
另一方面,如果这些文件中的每一个都有@class
Object1;或@class
Object2;,则没有循环引用。只要确保#import
将所需的头文件实际放入您的实现 (.m) 文件中即可。
For Example
例如
If you say @class myClass
, the compiler knows that it may see something like:
如果你说@class myClass
,编译器知道它可能会看到类似的东西:
myClass *myObject;
It doesn't have to worry about anything other than myClass is a valid class, and it should reserve room for a pointer to it (really, just a pointer). Thus, in your header, @class
suffices 90% of the time.
除了 myClass 是有效类之外,它不必担心任何其他事情,它应该为指向它的指针(实际上,只是一个指针)保留空间。因此,在您的标题中,@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. At this point (presumably in your implementation file), you'll need to #import "myClass.h"
, to tell the compiler additional information beyond just "this is a class".
但是,如果您需要创建或访问 myObject 的成员,则需要让编译器知道这些方法是什么。此时(大概在您的实现文件中),您需要#import "myClass.h"
告诉编译器除了“这是一个类”之外的其他信息。
回答by incmiko
As per Apple documentation :
根据 Apple 文档:
@class allows you to declare that a symbol is an Objective-c class name without the need to #import the header file that defines the class.
You would use this where you only need the class name defined for the purposes of declaring a pointer to the class or a method parameter of the class, and you do not need to access any methods, fields, or properties in the class.
It saves a minuscule amount of compile time vs the #import, and it sometimes helps avoid messy include circularity issues.
@class 允许你声明一个符号是一个 Objective-c 类名,而无需 #import 定义类的头文件。
您可以在只需要为声明类的指针或类的方法参数而定义的类名的情况下使用它,并且不需要访问类中的任何方法、字段或属性。
与#import 相比,它节省了极少的编译时间,并且有时有助于避免混乱的包含循环问题。
Great answer from here
来自这里的好答案
for example when you creat a protocol:
例如,当您创建协议时:
@class yourCustomView;
@protocol yourCustomViewDelegate <NSObject>
@required
- (void) somethingDidInView:(UIView*)view;
@end
@interface yourCustomView : FIView