Update: An updated version for XCode 4  is available. Please check it out here.

I have a habit of creating Singleton pattern classes for nearly every application I write. I manage the Settings Bundle (or any app settings, thru a Singleton class), my MKStoreManager for doing in-app purchases is a Singleton class. Even in Apple’s own code you can see some Singletons like UIApplication, Reachability (from the Reachability example), UIDevice and such. You normally access them through an exposed instance of the class. Ex: sharedApplication or currentDevice etc.,

The advantage of using a Singleton pattern is, the class completely isolates you from what it is supposed to do. For example, if you are using my MKStoreManager for doing your in-app purchases, you might have noticed that the four classes you downloaded will take care of all the in-app purchase nitty gritty code by completely encapsulating the Apple’s StoreKit. You have to write just 3-5 lines of code to make a real purchase.

Singleton is a very powerful way to create “global” classes that might otherwise end up in your AppDelegate. Matt Gallagher from Cocoawithlove has an excellent article on why cluttering your AppDelegate might be a problem, read it on his blog here.

Though it’s very easy to create a Singleton class, to make things even easier, I wrote a XCode template which is loosely based on Jeff Lamarche‘s NSOperation template.

Download the file from Attachments section. Extract the file from Command line using

unzip SingletonTemplate.zip -d /


You can download the file from here



This overwrites the built in TemplateChooser.plist file. If your XCode installation is vanilla, you don’t have to worry about this. But if you have installed any custom templates, you might lose them all.

When you are done, open XCode and click File -> New File.

You will see a prompt like this.

Choose Cocoa Touch classes and choose Objective-C class. You will see an additional option in the “Subclass of” combo box. Choose NSObject based Singleton and click finish.

You will get a boiler plate class that you can use it as as a Singleton.

Hope this is useful to you.

Update: An updated version for XCode 4  is available. Please check it out here.


Follow me on Twitter