I trying to implement singleton design pattern in objective C. Here is my code
In .h file
#import <Foundation/Foundation.h>
@interface BSCustomClass : NSObject
{
NSString *string;
}
@property (nonatomic,strong)NSString *string;
@end
In .m file
#import "BSCustomClass.h"
@implementation BSCustomClass
static int i;
static BSCustomClass* object;
@synthesize string;
-(id)init
{
if(i==0)
{
object=[super init];
i=1;
object.string=@"tunvir Rahman";
}
return object;
}
@end
Now if i want to create object of BSCustomClass from main using alloc and init then it will call is own init method and checks the static variable i. If i=0 then it is assumed that no object is created so far and create an object and after that it will return the address of the previous object for all object for the class BSCustomClass. Is this a correct implementation of singleton?? Thanks