Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在我的简单例子中,#import和@class之间的区别_Ios_Objective C_Uiviewcontroller_Forward Declaration - Fatal编程技术网

Ios 在我的简单例子中,#import和@class之间的区别

Ios 在我的简单例子中,#import和@class之间的区别,ios,objective-c,uiviewcontroller,forward-declaration,Ios,Objective C,Uiviewcontroller,Forward Declaration,在控制器的头文件中,我需要声明另一个控制器的实例。我是这样做的: #import "BIDMyRootController.h" #import "BIDAnotherController.h" //I import another controller's header @interface BIDCurrentController : BIDMyRootController //I declare an instance of another controller @property (

在控制器的头文件中,我需要声明另一个控制器的实例。我是这样做的:

#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
#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
上面的代码非常简单。没问题

但我也注意到,或者,我可以使用
@class
替换
#import
语句,以以下方式替换
另一个控制器的

#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
#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
之间有什么区别,如果它们都正常呢


更新:

顺便说一下,在
BIDCurrentController
的实现文件中,我再次导入了
BIDAnotherController

#import "BIDCurrentController.h"
#import "BIDAnotherController.h" //import another controller again
@implementation BIDCurrentController
...
@end
  • 使用
    @class BIDAnotherController
    被称为
    BIDAnotherController
    的前向声明,它基本上告诉编译器它的实现将在将来某个时候存在

  • #导入“BIDAnotherController.h”
    实际上将
    BIDAnotherController.h
    的内容包含到当前文件中

如果您只需要将
BIDAnotherController
用作方法的属性或参数,则可以不使用forward声明,因为您的代码不需要知道它存在以外的任何信息。如果您需要使用
BIDAnotherController
的属性或方法,则需要导入其头(否则编译器甚至不知道这些属性或方法存在!)

通常,转发声明用于中断两个或多个头文件之间的include循环。防止循环的最简单方法是选择
@class
声明,除非您确实需要访问类的属性或方法。

  • 使用
    @class BIDAnotherController
    被称为
    BIDAnotherController
    的前向声明,它基本上告诉编译器它的实现将在将来某个时候存在

  • #导入“BIDAnotherController.h”
    实际上将
    BIDAnotherController.h
    的内容包含到当前文件中

如果您只需要将
BIDAnotherController
用作方法的属性或参数,则可以不使用forward声明,因为您的代码不需要知道它存在以外的任何信息。如果您需要使用
BIDAnotherController
的属性或方法,则需要导入其头(否则编译器甚至不知道这些属性或方法存在!)


通常,转发声明用于中断两个或多个头文件之间的include循环。防止循环的最简单方法是首选
@class
声明,除非您确实需要访问某个类的属性或方法。

我添加了答案,但回忆起了这个问题。@class将类名声明为编译器的一种类型,以便您可以定义指向该类实例的指针。类的任何方法、属性或实例变量都没有声明,因此您不能在任何地方“使用”该类型的对象,只能传递它们的指针。可能的重复我添加了我的答案,但回想起了这个问题。@class将类名声明为编译器的类型,因此,您可以定义指向该类实例的指针。该类的方法、属性或实例变量都没有声明,因此您不能在任何地方“使用”该类型的对象,只传递它们的指针。这一切都很好。我只是想说,在可能的情况下使用前向声明是最好的做法,以便使代码尽可能模块化和封装。在大型项目中,进口产品很容易失控。在你意识到这一点之前,你已经有了不必要的导入、导入周期和疯狂的意大利面条依赖。如果一个类只需要知道另一个类的存在,但不调用它的任何方法或设置它的任何属性,请使用@class。谢谢大家,现在我很清楚,我会在2分钟内接受这个答案。这一切都很好。我只是想说,在可能的情况下使用前向声明是最好的做法,以便使代码尽可能模块化和封装。在大型项目中,进口产品很容易失控。在你意识到这一点之前,你已经有了不必要的导入、导入周期和疯狂的意大利面条依赖。如果一个类只需要知道另一个类的存在,但不调用它的任何方法或设置它的任何属性,请使用@class。谢谢大家,我现在很清楚,我会在2分钟内接受这个答案