design patterns - What should my Objective-C singleton look like? -


my singleton accessor method variant of:

static myclass *ginstance = null;  + (myclass *)instance {     @synchronized(self)     {         if (ginstance == null)             ginstance = [[self alloc] init];     }      return(ginstance); } 

what doing improve this?

another option use +(void)initialize method. documentation:

the runtime sends initialize each class in program 1 time before class, or class inherits it, sent first message within program. (thus method may never invoked if class not used.) runtime sends initialize message classes in thread-safe manner. superclasses receive message before subclasses.

so akin this:

static mysingleton *sharedsingleton;  + (void)initialize {     static bool initialized = no;     if(!initialized)     {         initialized = yes;         sharedsingleton = [[mysingleton alloc] init];     } } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -