views:

26927

answers:

6

It is to my understanding that one should use a forward-class declaration in the event ClassA needs to include a ClassB header, and ClassB needs to include a ClassA header to avoid any circular inclusions. I also understand that an #import it a simple ifndef so that an include only happens.

My inquiry is this. When does one use #import and when does one use @class? Sometimes if I use a @class declaration, I see a common compiler warning such as the following:

warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.

Would really love to understand this, versus just removing the @class forward-declaration and throwing an #import in to silence the warnings the compiler is giving me.

+17  A: 

Use a forward declaration in the header file if needed, and #import the header files for any classes you're using in the implementation. In other words, you always #import the files you're using in your implementation, and if you need to reference a class in your header file use a forward declaration as well.

The exception to this is that you should #import a class or formal protocol you're inheriting from in your header file (in which case you wouldn't need to import it in the implementation).

Marc Charbonneau
+96  A: 

If you see this warning:

warning: receiver 'myCoolClass' is a forward class and corresponding @interface may not exist

you need to #import the file, but you can do that in your implementation file (.m), and use the @class declaration in your header file.

@class does not (usually) remove the need to #import files, it just moves the requirement down closer to where the information is useful.




For Example
If you say "@class myCoolClass", the compiler knows that it may see something like:

myCoolClass     *myObject;

It doesn't have to worry about anything other than myCoolClass 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.

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 "myCoolClass.h", to tell the compiler additional information beyond just "this is a class".

Ben Gottlieb
Great answer, thanks. For future reference: this also deals with situations where you `@class` something in your `.h` file, but forget to `#import` it in the .m, try to access a method on the `@class`ed object, and get warnings like: `warning: no -X method found`.
Tim
A case where you'd need to #import instead of @class is if the .h file includes data types or other definitions necessary for your class's interface.
Ken Aspeslagh
+6  A: 

The common practice is using @class in header files (but you still need to #import the superclass), and #import in implementation files. This will avoid any circular inclusions, and it just works.

Steph Thirion
I thought #import was better than #Include in that it only imports one instance?
Matthew Schinckel
True. Don't know if it's about circular inclusions, or incorrect ordering, but I adventured away from that rule (with one import in a header, imports were no longer needed in subclasse's implementation), and soon it got really messy. Bottom line, follow that rule, and the compiler will be happy.
Steph Thirion
+18  A: 

Look at the Objective-C Programming Language documentation on ADC

Under the section on Defining a Class | Class Interface it describes why this is done:

The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.

I hope this helps.

Abizern
+20  A: 

Three simple rules:

  • Only #import the super class in header files.
  • #import all classes you send messages to in implementation.
  • Forward declarations for everything else.

If you do forward declaration in the implementation files, then you probably do something wrong.

PeyloW
+1  A: 

Another advantage: Quickly compilation

If you include a header file any change in it causes the current file also to compile but this is not the case if the class name is declared as @classname

Venkateshwar