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

著書紹介

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

目次