title: Singleton in iOS 5?
body: <p>Hi I had an implementation previous versions of iOS for a singleton as follows:</p>
  
  <p>.h file</p>
  
  <pre><code>@interface CartSingleton : NSObject 
  {
  
  }
  +(CartSingleton *) getSingleton;
  </code></pre>
  
  <p>.m file</p>
  
  <pre><code>@implementation CartSingleton
  
  static CartSingleton *sharedSingleton = nil;
  
  +(CartSingleton *) getSingleton
  {
      if (sharedSingleton !=nil)
         {
          NSLog(@"Cart has already been created.....");
          return sharedSingleton;
         }
      @synchronized(self)
     {
      if (sharedSingleton == nil)
         {
          sharedSingleton = [[self alloc]init];
          NSLog(@"Created a new Cart");
         }
     }
      return sharedSingleton;
  }
  //==============================================================================
  +(id)alloc
  {
      @synchronized([CartSingleton class])
     {
      NSLog(@"inside alloc");
      NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
      sharedSingleton = [super alloc];
      return sharedSingleton;
     }
  
      return nil;
  }
  
  //==============================================================================
  -(id)init
  {
      self = [super init];
  }
  </code></pre>
  
  <p>However on the web I see people have implemented the Singleton design pattern using this code:</p>
  
  <pre><code>+ (id)sharedInstance
  {
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&amp;pred, ^{
      _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
  }
  </code></pre>
  
  <p>Could someone who is experience please guide me.
  Im a newbie and thoroughly confused between the old iOS implementation of the Singleton and the new one and which is the correct one?</p>
  
  <p>Thanks a lot</p>