@class在iOS 4开发中做了什么? [英] what does @class do in iOS 4 development?
问题描述
执行中是否有任何区别
@class MyViewController;
而不是将.h正常导入appdelegate.h
rather than doing the normal import of the .h into the appdelegate.h
#import "MyViewController.h"
我最近看到一些使用@class方式的例子,想知道是否有任何差异。
I've seen some example recently that use the @class way and wondered if there any differences.
谢谢。
推荐答案
有很大的不同。
@class MyViewController;
是对象的前向声明 MyViewController
。当你只需要告诉编译器一个对象类型但不需要包含头文件时就可以使用它。
Is a forward declaration for the object MyViewController
. It is used when you just need to tell the compiler about an object type but have no need to include the header file.
但是你需要创建一个这样的对象在它上面键入和调用方法,你需要:
If however you need to create an object of this type and invoke methods on it, you will need to:
#import "MyViewController.h"
但通常这是在 .m
文件中完成的。
But normally this is done in the .m
file.
当您在与使用它的对象相同的头文件中定义 @protocol
时,另外使用前向声明。 / p>
An additional use of forward declarations is when you define a @protocol
in the same header file as an object that uses it.
@protocol MyProtocolDelegate; //forward declaration
@interface MyObject {
id<MyProtocolDelegate> delegate;
...
}
...
@end
@protocol MyProtocolDelegate
... //protocol definition
@end
在上面的例子中,编译器需要知道 @协议MyProtocolDelegate
在编译 MyObject
对象之前有效。
In the above example the compiler needs to know that the @protocol MyProtocolDelegate
is valid before it can compile the MyObject
object.
只需移动上面的协议定义 MyObject
定义也可以。
Simply moving the protocol definition above MyObject
definition would also work.
这篇关于@class在iOS 4开发中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!