Singleton class in Objective c

The use of singleton class is used to reuse the stuff which is declared in the single ton class.with the help of a singleton class the object for a class created only one and can be used through out the project.

Here is the syntax for singleton class..

the string can be accessed by

labl.text=[[SingleTonClass singleTonMethodObj]singletonClassString];

+(SingleTonClass*)singleTonMethodObj
{
static SingleTonClass *singleTonMethodObj=nil;
if (!singleTonMethodObj) {
singleTonMethodObj=[[super allocWithZone:nil]init];
}
return singleTonMethodObj;
}

+(id)allocWithZone:(NSZone *)zone
{

return [self singleTonMethodObj];
}

-(id)init
{
self= [super init];
if(self)
{
singletonClassString=@”Hii am from singleTon Class”;
}

return self;

}

 

 

 

The string declared here is the singletonClassString   can be used in all other classes by using the following syntax

[[SingleTonClass singleTonMethodObj]singletonClassString];

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s