– シングルトン パターンの生成と解放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | static hoge *sInstance = nil; @implementation hoge + (id)sharedInstance { @synchronized(self) { // スレッドセーフとする if (!sInstance) { sInstance = [[self alloc] init]; } } return sInstance; } + (void)releaseInstance { [sInstance release]; sInstance = nil; // インスタンスにアクセスされても問題がないように nil をセットする } @end |