0

I am new to iOS development and I have gone through singleton class. I understood the concept, but having doubts in implementing the singleton class. Can anyone please share source code of the real time example using singleton class.

Naveen
  • 49
  • 9
  • if you are interested in learning another pattern, i find [this](http://www.raywenderlich.com/46988/ios-design-patterns) helpful for understanding and recognise the pattern in iOS app. – seto nugroho Nov 24 '15 at 06:44

4 Answers4

1

This is how a GCD for singleton class looks like. Suppose there is a class that you made, MySingleTonClass which is a subclass of NSObject

MySingleTonClass.h

 +(instanceType)sharedManager;
@property (nonatomic, strong) NSString *userName;

MySingleTonClass.m

+(instanceType)sharedManager{
     static MySingleTonClass *manager = nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
        manager = [[MySingleTonClass alloc]init];
     });
     return manager;
}

Now you call this singleTon Class in some other class suppose in ViewController.m. First Import the Class

   #import MySingleTonClass.h

 -(void)viewDidLoad{

      MySingleTonClass *manager = [MySingleTonClass sharedManager];
      manager.userName = @"ABCDE";
       //manager is the singleton Object
  }

Edit

Now suppose you want to access this same value. then suppose in some other ViewController, after ViewController

Suppose in SecondViewController.m

  #import "MySingleTonClass.h"

   -(void)viewDidLoad{

      MySingleTonClass *manager = [MySingleTonClass sharedManager];
      NSLog (@"%@",manager.userName);
      // This would still log ABCDE, coz you assigned it the class before, So even if you create a new object called manager here, it will return the same Manager you created before.

    manager.userName = @"Myname"; //Now the value changed to MyName untill you change it again, in the lifetime of this application.
  }

I hope i could make you understand the concept of it.

As you know, dispatch_once_t is a GCD snippet that makes the code inside of it invoke only ONCE per application run. Any code you write inside it will be run, or rather invoked only once in the lifetime of the application being active.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

Check out this link for the original source - http://getsetgames.com/2009/08/30/the-objective-c-singleton/

@implementation MySingleton
static MySingleton* _sharedMySingleton = nil;

+(MySingleton*)sharedMySingleton
{
    @synchronized([MySingleton class])
    {
        if (!_sharedMySingleton)
            [[self alloc] init];

        return _sharedMySingleton;
    }

    return nil;
}
Saumil Shah
  • 2,299
  • 1
  • 22
  • 27
0
static User *defaultUser;

+ (User *)defaultUser
{
    if (!defaultUser)
    {
        defaultUser = [self new];

        // do something...
    }


    return defaultUser;
}
yanyabo111
  • 285
  • 1
  • 4
0
There are two ways:-
1) We can create singleton class using **GCD** dispatch_once 
in this only one object will create if existing object is there then it will refer to them. 

 +(id)sharedManager 
{
    static MyManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}
2) Second way is follows:-

+ (id)sharedManager {
    static MyManager *sharedMyManager = nil;
    @synchronized(self) {
        if (sharedMyManager == nil)
            sharedMyManager = [[self alloc] init];
    }
    return sharedMyManager;
}

suppose this above method is written in class **MyManager** then  u can use that  as follow       
 MyManager *sharedManager = [MyManager sharedManager];
hope this will help u.
user3306145
  • 76
  • 2
  • 12