I had received the question about the forward class declaration of the Objective-C and this is the answer.
If you want to declare the forward class declaration, you can write following:
[cc lang=”objc”]
@class MyObject;
[/cc]
The member variable of the Objective-C class needs the class name is declared before the variable is appeared. In this case, you can write the “#import” in the header file and read the declarations. For example, this code:
[cc lang=”objc”]
#import “Cocoa/Cocoa.h”
#import “MyObject.h”
@interface MyObjectB : NSObject {
MyObject *_myObject;
}
@end
[/cc]
This code could be error when you write the following code in the “MyObject.h” file.
[cc lang=”objc”]
#import “Cocoa/Cocoa.h”
#import “MyObjectB.h”
@interface MyObject : NSObject {
MyObjectB *_object;
}
@end
[/cc]
Following errors will be occurred when you complie abobe code.
[cc]
Complie MyObject.m
Expected specifier-qualifer-list before “MyObject”
Compile MyObjectB.m
Expected specifier-qualifier-list before “MyObjectB”
[/cc]
The meaning of these errors are:
- The declaration of the “MyObject” is not appeared in compling “MyObject.m” file (The “MyObject.m” file imports the “MyObject.h” file).
- The declaration of the “MyObjectB” is not appeared in compling “MyObject.b” file (The “MyObjectB.m” file imports the “MyObjectB.h” file)
Why the complier says the declration is not appeared? The declration is imported with “#import”.
You can know the reason by thinking how the preprocessor works. The preprocessor does following.
“MyObject.h” file
- Imports “Cocoa/Cocoa.h” file.
- Imports “MyObjectB.h” file (The “MyObject” class is not declared at this point)
- Try to imports the “MyObject.h” file at the line “#import “MyObject.h””, but the file is imported only one time when you use the “#import” so the line “#import “MyObject.h”” will be ignored.
- The line “MyObject *_objct;” is appered but the class declration of the “MyObject” class is not appered so the compiler will stop.
“MyObjectB.h” file
- Imports “Cocoa/Cocoa.h” file.
- Imports “MyObject.h” file (The “MyObjectB” class is not declared at this point)
- Try to imports the “MyObjectB.h” file at the line “#import “MyObjectB.h””, but the file is imported only one time when you use the “#import” so the line “#import “MyObjectB.h”” will be ignored.
- The line “MyObjectB *_objct;” is appered but the class declration of the “MyObjectB” class is not appered so the compiler will stop.
To avoid this problem, you can use the “@class”. Above code can be written following.
“MyObject.h” file
[cc lang=”objc”]
#import “Cocoa/Cocoa.h”
//#import “MyObjectB.h”
@class MyObjectB;
@interface MyObject : NSObject {
MyObjectB *_object;
}
@end
[/cc]
“MyObjectB.h” file
[cc lang=”objc”]
#import “Cocoa/Cocoa.h”
//#import “MyObject.h”
@class MyObject;
@interface MyObjectB : NSObject {
MyObject *_myObject;
}
@end
[/cc]
There is the foward protocol declration:
[cc lang=”objc”]
@protocol ProtocolName;
[/cc]
Either you write the protocol declation before or after the class declation, the compiler reports error. You can avoid this problem by using the foward protocol declaration.
The sample code is available.
RecursiveImport