【发布时间】:2013-05-01 14:48:57
【问题描述】:
在我的控制器的头文件中,我需要声明另一个控制器的实例。我是通过以下方式做到的:
#import "BIDMyRootController.h"
#import "BIDAnotherController.h" //I import another controller's header
@interface BIDCurrentController : BIDMyRootController
//I declare an instance of another controller
@property (strong, nonatomic) BIDAnotherController *anotherController;
@end
上面的代码非常简单。没问题!
但我也注意到,或者,我可以使用 @class 以下列方式替换我的 #import 语句为 BIDAnotherController:
#import "BIDMyRootController.h"
@class BIDAnotherController //I declare another controller with @class tag
@interface BIDCurrentController : BIDMyRootController
//I declare an instance of another controller
@property (strong, nonatomic) BIDAnotherController *anotherController;
@end
也没问题!
但我现在很困惑,#import "BIDAnotherController.h" 和@class BIDAnotherController 有什么区别那么如果它们都ok???
更新:
对了,在BIDCurrentController的实现文件中,我又导入了BIDAnotherController:
#import "BIDCurrentController.h"
#import "BIDAnotherController.h" //import another controller again
@implementation BIDCurrentController
...
@end
【问题讨论】:
-
@class vs. #import 的可能重复项我添加了我的答案,但又想起了这个问题..
-
@class 将类名声明为编译器的类型,因此您可以定义指向该类实例的指针。没有声明类的方法、属性或实例变量,因此您不能在任何地方“使用”该类型的对象,只能传递它们的指针。
标签: ios objective-c uiviewcontroller forward-declaration