Last Updated: February 25, 2016
· jamesbrooks
317471 10150285147655308 136807138 n

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.

Say Thanks