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

メッセージを受信した側のクラスの新しいインスタンスを返すクラスメソッドです。
オブジェクトの割り当てなどが行われます。
初期化プロセスを完了させるには、継承されたクラスに定義されているinit...メソッドを使用します。

+ (id)alloc


戻り値1 = [ 名前1 alloc ]

戻り値1
id
名前1のクラスの新しいインスタンス
名前1
インスタンスを作成するクラス名

(例)
※ 「***_Prefix.pch」などのファイルの内容は記載しません。

MyTestClass.h
#import <Foundation/Foundation.h> @interface MyTestClass : NSObject - (id)init; @end

MyTestClass.m
#import "MyTestClass.h" @implementation MyTestClass static long order = 1; - (id)init { self = [super init]; if (self) { NSLog(@"%ld番目インスタンスの初期化完了\n", order++); } return self; } @end

main.m
#import "MyTestClass.h" int main (void) { MyTestClass *mtc1 = [[MyTestClass alloc] init]; MyTestClass *mtc2 = [[MyTestClass alloc] init]; return 0; }

実行結果
1番目インスタンスの初期化完了 2番目インスタンスの初期化完了