Loose-Info.com
Last Update 2013/02/13
TOP - Objective-C - フレームワーク - Foundation - NSObject - +initialize

クラスが使用される(メッセージを受ける)前に、このメソッドが実行されます。
サブクラスが使用される場合は、スーパークラスのinitializeメソッドが先に実行されます。
このメソッドは1回だけ実行されます。

+ (void)initialize

実装されている場合、自動的に実行

(例)
※ Xcodeで生成される「***_Prefix.pch」などのファイルの内容は記載しません。

実行例[1] サブクラスを呼び出した場合のスーパークラスの+initializeの実行
#import <Foundation/Foundation.h> @interface MyTestClass1 : NSObject + (void)initialize; @end @implementation MyTestClass1 + (void)initialize { NSLog(@"MyTestClass1 initialize"); } @end @interface MyTestClass2 : MyTestClass1 + (void)initialize; @end @implementation MyTestClass2 + (void)initialize { NSLog(@"MyTestClass2 initialize"); } @end int main (void) { NSLog(@"クラス呼び出し前\n"); MyTestClass2 *test = [[MyTestClass2 alloc] init]; return 0; }

実行結果
クラス呼び出し前 MyTestClass1 initialize MyTestClass2 initialize


実行例[2] サブクラスを先に呼び出した場合のスーパークラスの+initializeの挙動
#import <Foundation/Foundation.h> @interface MyTestClass1 : NSObject + (void)initialize; @end @implementation MyTestClass1 + (void)initialize { NSLog(@"MyTestClass1 initialize"); } @end @interface MyTestClass2 : MyTestClass1 + (void)initialize; @end @implementation MyTestClass2 + (void)initialize { NSLog(@"MyTestClass2 initialize"); } @end int main (void) { NSLog(@"クラス呼び出し前\n"); MyTestClass2 *test2 = [[MyTestClass2 alloc] init]; MyTestClass1 *test1 = [[MyTestClass1 alloc] init]; // サブクラスの呼び出しの際に呼び出されているため再度の呼び出しは無し return 0; }

実行結果
クラス呼び出し前 MyTestClass1 initialize MyTestClass2 initialize

実行例[3] +initializeが実装されていないサブクラスを呼び出した場合のスーパークラスの+initializeの挙動

スーパークラスの為の呼び出し + 未実装クラス(MyTestClass3)の呼び出しにより、MyTestClass2の+initializeが2回呼び出される
#import <Foundation/Foundation.h> @interface MyTestClass1 : NSObject + (void)initialize; @end @implementation MyTestClass1 + (void)initialize { NSLog(@"--------------------------"); NSLog(@"MyTestClass1 initialize"); NSLog(@"[self description] → %@", [self description]); NSLog(@"[[MyTestClass1 class] description] → %@", [[MyTestClass1 class] description]); } @end @interface MyTestClass2 : MyTestClass1 + (void)initialize; @end @implementation MyTestClass2 + (void)initialize { NSLog(@"--------------------------"); NSLog(@"MyTestClass2 initialize"); NSLog(@"[self description] → %@", [self description]); NSLog(@"[[MyTestClass2 class] description] → %@", [[MyTestClass2 class] description]); } @end @interface MyTestClass3 : MyTestClass2 // +initializeの実装無し @end @implementation MyTestClass3 @end int main (void) { NSLog(@"クラス呼び出し前\n"); MyTestClass3 *test3 = [[MyTestClass3 alloc] init]; return 0; }

実行結果
クラス呼び出し前 -------------------------- MyTestClass1 initialize [self description] → MyTestClass1 [[MyTestClass1 class] description] → MyTestClass1 -------------------------- MyTestClass2 initialize [self description] → MyTestClass2 [[MyTestClass2 class] description] → MyTestClass2 -------------------------- MyTestClass2 initialize [self description] → MyTestClass3 [[MyTestClass2 class] description] → MyTestClass2

実行例[4] +initializeに複数実行回避のためのコードを追加

※ デヴェロッパドキュメント内の NSObject Class Reference よりコードにてテスト
#import <Foundation/Foundation.h> @interface MyTestClass1 : NSObject + (void)initialize; @end @implementation MyTestClass1 + (void)initialize { if ( self == [MyTestClass1 class] ) { NSLog(@"--------------------------"); NSLog(@"MyTestClass1 initialize"); NSLog(@"[self description] → %@", [self description]); NSLog(@"[[MyTestClass1 class] description] → %@", [[MyTestClass1 class] description]); } } @end @interface MyTestClass2 : MyTestClass1 + (void)initialize; @end @implementation MyTestClass2 + (void)initialize { if ( self == [MyTestClass2 class] ) { NSLog(@"--------------------------"); NSLog(@"MyTestClass2 initialize"); NSLog(@"[self description] → %@", [self description]); NSLog(@"[[MyTestClass2 class] description] → %@", [[MyTestClass2 class] description]); } } @end @interface MyTestClass3 : MyTestClass2 // +initializeの実装無し @end @implementation MyTestClass3 @end int main (void) { NSLog(@"クラス呼び出し前\n"); MyTestClass3 *test3 = [[MyTestClass3 alloc] init]; return 0; }

実行結果
クラス呼び出し前 -------------------------- MyTestClass1 initialize [self description] → MyTestClass1 [[MyTestClass1 class] description] → MyTestClass1 -------------------------- MyTestClass2 initialize [self description] → MyTestClass2 [[MyTestClass2 class] description] → MyTestClass2