title: Correct Singleton Pattern Objective C (iOS)?
body: <p>I found some information in the net to create a singleton class using GCD. Thats cool because it's thread-safe with very low overhead. Sadly I could not find complete solutions but only snippets of the sharedInstance method. So I made my own class using the trial and error method - and et voila - the following came out:</p>
  
  <pre><code>@implementation MySingleton
  
  // MARK: -
  // MARK: Singleton Pattern using GCD
  
  + (id)allocWithZone:(NSZone *)zone { return [[self sharedInstance] retain]; }
  - (id)copyWithZone:(NSZone *)zone { return self; }
  - (id)autorelease { return self; }
  - (oneway void)release { /* Singletons can't be released */ }
  - (void)dealloc { [super dealloc]; /* should never be called */ }
  - (id)retain { return self; }
  - (NSUInteger)retainCount { return NSUIntegerMax; /* That's soooo non-zero */ }
  
  + (MySingleton *)sharedInstance
  {
      static MySingleton * instance = nil;
  
      static dispatch_once_t predicate;   
      dispatch_once(&amp;predicate, ^{
          // --- call to super avoids a deadlock with the above allocWithZone
          instance = [[super allocWithZone:nil] init];
      });
  
      return instance;
  }
  
  // MARK: -
  // MARK: Initialization
  
  - (id)init
  {
      self = [super init];
      if (self) 
      {
          // Initialization code here.
      }
      return self;
  }
  
  @end
  </code></pre>
  
  <p>Please feel free to comment and tell me if I've missing something or doing something completely wrong ;)</p>
  
  <p>Cheers
  Stefan</p>