My singleton accessor method is usually some variant of:
static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance = [[self alloc] init]; } return(gInstance); }
What could I be doing to improve this?
Another option is to use the
+(void)initializemethod. From the documentation:So you could do something akin to this: