Monday, November 4, 2013

What is Singleton class?

A Singleton class returns the same instance no matter how many times an application requests it. 
Example: UIApplication, UIAccelerometer, NSFileManager etc

For making our custom class(say Car) as singleton we need to create a class method as below:

+(id)sharedCar{
 static id singleInstance=nil;
if(singleInstance==nil){
singleInstance =[[Car alloc]init];
}
 return singleInstance;
}

No comments:

Post a Comment