Question

I want to know the porper way to create the singleton class.Currently I am doing in the following way. In the .h file I am declaring the instance of the class in the following way.

@interface GlobalMethods : NSObject
{
}
+ (GlobalMethods *)sharedInstance;

And in the .m file I did the code in the following way.

@implementation GlobalMethods
- (id)init
{
self = [super init];
if (self)
{
    //Custom Intialization
}

return self;
}

+ (GlobalMethods *)sharedInstance
{
     static GlobalMethods *sharedInstance = nil;
     @synchronized(self)
     {
         if (sharedInstance == NULL)
         {
              sharedInstance = [[GlobalMethods alloc] init];
         }
      return sharedInstance;
 }

}

Was it helpful?

Solution

we can create only one instance from that singleton class.
take a look at this page for more information with code :

    http://www.galloway.me.uk/tutorials/singleton-classes/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top