2

In my controller's header file, I need to declare a instance of another controller. I did it in the following way:

#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

The above code is pretty straight forward. No problem!

But I also noticed that, alternatively, I can use @class to replace my #import statement for BIDAnotherController in the following way:

#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

No problem too!

But I am confused now, what are the differences between #import "BIDAnotherController.h" and @class BIDAnotherController then if they are both ok???


Update:

By the way, in the implementation file of BIDCurrentController, I have imported BIDAnotherController again:

#import "BIDCurrentController.h"
#import "BIDAnotherController.h" //import another controller again
@implementation BIDCurrentController
...
@end
jszumski
  • 7,430
  • 11
  • 40
  • 53
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • possible duplicate of [@class vs. #import](http://stackoverflow.com/questions/322597/class-vs-import) I added my answer, but recalled this question.. – Anoop Vaidya May 01 '13 at 14:53
  • @class declares the class name as a type to the compiler, so you can define pointers to instances of that class. None of the methods, properties, or instance vars of the class are declared and hence you cannot "use" objects of that type anywhere, only pass their pointers around. – Hot Licks May 01 '13 at 15:55

1 Answers1

4
  • Using @class BIDAnotherController is called a forward declaration of BIDAnotherController, which basically tells the compiler that an implementation for it will exist at some point in the future.

  • #import "BIDAnotherController.h" actually includes the contents of BIDAnotherController.h into the current file.

If you only need to use BIDAnotherController as a property or argument to a method, you can get away with the forward declaration because your code doesn't need to know anything about it other than it exists. If instead you need to use properties or methods of BIDAnotherController then you'll need to import its header (otherwise the compiler won't know those properties or methods even exist!).

Typically forward declarations are used to break an include cycle between two or more header files. The easiest way to prevent a cycle is to prefer @class declarations unless you really do need access to properties or methods of a class.

jszumski
  • 7,430
  • 11
  • 40
  • 53
  • 1
    This is all good. I'd just that that it's best practice to use forward declarations when you can, in order to keep your code as modular and encapsulated as possible. It is *very* easy for #imports to get out of hand on a larger project. Before you know it, you have unnecessary imports, import cycles, and crazy spaghetti dependencies. If a class just needs to know about the existence of another class, but not call any of its methods or set any of its properties, use @class. – Reid May 01 '13 at 14:59
  • THanks all, it is clear to me now , I'll accept this answer in 2min – Mellon May 01 '13 at 15:00