Last Updated: February 25, 2016
· jamesbrooks

Objective-C Singleton

Want a shared instance of a class without putting everything in the application's delegate? Use a singleton class!

The following method returns a shared singleton instance of the class in question. Additional creation logic can be added, but make sure to place it inside the dispatch_once block.

+ (__class__ *)sharedInstance {
  static __class__ *_obj = nil;
  static dispatch_once_t onceQueue;

  dispatch_once(&onceQueue, ^{
    _obj = [[__class__ alloc] init];

  return _obj;

Replace __class__ with the actual class name.