The Problem if you don't know the forward class declaration of the Objective-C

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

  1. Imports “Cocoa/Cocoa.h” file.
  2. Imports “MyObjectB.h” file (The “MyObject” class is not declared at this point)
  3. 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.
  4. 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

  1. Imports “Cocoa/Cocoa.h” file.
  2. Imports “MyObject.h” file (The “MyObjectB” class is not declared at this point)
  3. 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.
  4. 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

関連記事

  1. Gitへの移行 – その2

  2. Lionを入れるには非力すぎた

  3. MultiTextConverter は Mac OS X 10.6に…

  4. 今日は雪です

  5. [お知らせ] C言語逆引きハンドブックのPDF版の販売が始まりました

  6. 長く運用するサーバーは仮想マシンにしておこう

最近の著書

最近の記事