└── README /README: -------------------------------------------------------------------------------- 1 | If your system supports blocks (like iOS 4+) then you should just use the dispatch_once function as follows: 2 | 3 | + (MyClass *)sharedInstance 4 | { 5 | static MyClass *sharedInstance = nil; 6 | static dispatch_once_t onceToken; 7 | dispatch_once(&onceToken, ^{ 8 | sharedInstance = [[MyClass alloc] init]; 9 | // Do any other initialisation stuff here 10 | }); 11 | return sharedInstance; 12 | } 13 | 14 | This method was sourced from http://stackoverflow.com/questions/7568935/how-do-i-implement-an-objective-c-singleton-that-is-compatible-with-arc courtesy Nick Forge 15 | 16 | 17 | =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 18 | =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= 19 | 20 | 21 | See the "pre-blocks" branch for the old implementation: 22 | 23 | A Singleton Macro that swaps method implementations at runtime so that only the initialization portion will require syncrhonization (lock). 24 | - Specifically it uses method_setImplementation() to dynamically replace the sharedInstance access method with one that does not instantiate a new object and thus does not require @synchronized. 25 | 26 | It is based on the work here: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html by Matt Gallagher 27 | But changing the implementation to use method swizzling as described here: http://googlemac.blogspot.com/2006/11/synchronized-swimming-part-2.html by Dave MacLachlan of Google. 28 | --------------------------------------------------------------------------------