, , :
static MyClass* theInstance = nil;
+ (MyClass*) sharedInstance {
@synchronized(self) {
if (! theInstance) {
theInstance = [[MyClass alloc] init];
}
}
return theInstance;
}
... , , [MyClass sharedInstance], .
, , alloc init . , init , , / alloc init.
In practice, however, singleton classes most often use the approach sharedInstance(sometimes with minor variations in the name, for example [UIApplication sharedApplication]).
source
share