Singletonを使う場面はよくあるんですが、iOS4以降はGrand Central Dispatchがあるのでそれのdispatch_o
GCD 前:
1 + (MyController*)singleton { 2 static MyControll er *controller = nil; 3 4 if(!controller ) { 5 controller = [[MyControll er] alloc] init]; 6 } 7 return controller ; 8 }
GCD 後:
1 + (MyController*)singleton { 2 static dispatch_o nce_t pred = 0; 3 static MyControll er *controller = nil; 4 5 dispatch_o nce(&pred, ^{ 6 controller = [[MyControll er alloc] init]; 7 }); 8 return controller ; 9 }
Cocoa Fundamenta
Cocoa Fundamenta
ls Guide
http://developer.app le.com/lib rary/mac/#documentat ion/Cocoa/ Conceptual /CocoaFund amentals/C ocoaObject s/CocoaObj ects.html#//apple_re f/doc/uid/ TP40002974 -CH4-SW32
また、Apple Developer Forumsのこの質問もとても勉強になります:
Apple Developer Forums
https://devforums.ap ple.com/me ssage/4550 02#455002
下記も参考
Singletons
: You're doing them wrong
http://cocoasamurai. blogspot.c om/2011/04 /singleton s-your-doi ng-them-wr ong.html
posted by
satoko
on Tue 28 Jun 2011
at 11:21