Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
30 lines (15 sloc) 1.54 KB

Simple Singletons

This project allows you to easily make a class a singleton in Objective-C. With this project you can write:

@interface FRDatabaseManager : NSObject <FRSingleton>

And your class is now a singleton -- that's all there is to it!

This method works by returning your singleton object from the alloc method. For convenience and clarity, you can also implement an accessor for the singleton:

@implementation FRDatabaseManager
+ (id)sharedDatabaseManager { return [[self alloc] autorelease]; }


The project was first presented at SpikedCocoa as an example of using the Objective-C runtime to our advantage. This project is by no means intended to promote wide use of the singleton design pattern. You should be aware of the drawbacks that singletons have before using them. If you decide to use singletons, though, this will significantly decrease the code you write and you'll have a better, more consistent implementation.


Singleton support is achieved by this project by swizzling the NSObject::allocWithZone: method. The implementation takes into consideration the safety and speed implications of swizzling this method.


This code is compatible with Mac OS X 10.6 and later. It is most optimized on Mac OS X 10.7, as it is able to use IMP_implementationWithBlock. It will compile with gcc and clang and works with Automatic Reference Counting (ARC) as well as Garbage Collection.