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

投稿者プロフィール

林 晃
林 晃アプリ開発者
アールケー開発代表。Appleプラットフォーム向けのアプリ開発が好きなアプリ開発者。アプリの受託開発、技術書執筆、技術指導・セミナー講師。3DCGコンテンツ作成にも取組中です。

基礎から学ぶARKit


「基礎から学ぶARKit」を執筆しました。本書はARKitを使ったARアプリの開発方法を解説した技術書です。

ARKitを使ってARアプリを作るときの流れや基本的なAPIの使い方などをサンプルアプリを作りながら学べます。

詳細

基礎から学ぶMetal


「基礎から学ぶMetal」を執筆しました。本書はMetalを使ってGPUプログラミングを行うための最初のステップを解説するMetalの解説書です。

私が初めてGPUプログラミングを行ったとき、どこから手をつけて、学んでいけば良いのか分からず呆然としました。もし、あのとき、これを教えてくれればという部分を解説しました。本書で解説している部分はMetalの基礎となる部分で、Metalを使うときに必ず触れることになる部分です。

詳細

関連記事

  1. FileVaultとバックアップ

  2. StoryboardやXibは使わない方が良いかも?

  3. Swiftではコードのエラー判定は強めに

  4. RKDetailDesign 1.6.2 を公開しました

  5. OSSのリポジトリで英語学習と技術学習

  6. VMware上へ移行

最近の著書

  1. 基礎から学ぶ SwiftUI

最近の記事