I'd like to create a Singleton with ARC,
this is the answer I see.
Is there anyway to convert this code to something similar without using block?
+ (MyClass *)sharedInstance
{
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MyClass alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}
EDIT:
I just see this approach:
static MyClass *sharedMyClassInstance = nil;
+(MyClass *) sharedMyClass
{
@synchronized(self) {
if (sharedMyClassInstance == nil) {
sharedMyClassInstance = [[self alloc] init];
}
return sharedMyClassInstance;
}
}
will this prevent the object created more than one?