Having just read Why does Apple recommend to use dispatch_once for implementing the singleton pattern under ARC?, I find the answers and approach Apple recommends for singletons to be extremely cool and neat to look at, but after further thought I was left wondering, what does the static keyword inside of a class method mean exactly in objective-c? Prior to this pattern recommended by Apple, I had only encountered static as a modifier for class fields. How does the behavior change when static is used in a class method?
+ (MyClass *)sharedInstance
{
// Static local predicate must be initialized to 0
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
sharedInstance = [[MyClass alloc] init];
// Do any other initialisation stuff here
});
return sharedInstance;
}