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 t
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.
This is an example scenario, where we need @class.
Consider if you wish to create a protocol within header file, which has a parameter with data type of the same class, then you can use @class. Please do remember that you can also declare protocols separately, this is just an example.
// DroneSearchField.h
#import <UIKit/UIKit.h>
@class DroneSearchField;
@protocol DroneSearchFieldDelegate<UITextFieldDelegate>
@optional
- (void)DroneTextFieldButtonClicked:(DroneSearchField *)textField;
@end
@interface DroneSearchField : UITextField
@end
for extra info about file dependencies & #import & @class check this out:
http://qualitycoding.org/file-dependencies/ itis good article
summary of the article
imports in header files:
- #import the superclass you’re inheriting, and the protocols you’re implementing.
- Forward-declare everything else (unless it comes from a framework with a master header).
- Try to eliminate all other #imports.
- Declare protocols in their own headers to reduce dependencies.
- Too many forward declarations? You have a Large Class.
imports in implementation files:
- Eliminate cruft #imports that aren’t used.
- If a method delegates to another object and returns what it gets back, try to forward-declare that object instead of #importing it.
- If including a module forces you to include level after level of successive dependencies, you may have a set of classes that wants to become a library. Build it as a separate library with a master header, so everything can be brought in as a single prebuilt chunk.
- Too many #imports? You have a Large Class.
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).
When I develop, I have only three things in mind that never cause me any problems.
For all other classes (subclasses and child classes in my project self), I declare them via forward-class.
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.