Online about the iOS singleton mode implementation of the post has been many, there are many versions, there are right there is also wrong. I am in the use of the process is difficult to find a more perfect method, simply write a bar, after the project validation is a more reasonable version.
StaticPrautologinview *s_sharedinstance =Nil;+ (Prautologinview *) shareinstance{@synchronized (self) {if(S_sharedinstance = =Nil) {s_sharedinstance= [[[Selfclass] [ hidealloc] init]; } } returns_sharedinstance;}#pragmaMark--#pragmaMark Singleton apis+(ID) hidealloc{return[Super alloc];}+(ID) alloc//completely block out the Alloc function {Nsassert (1==0,@"[prautologinview]please use +shareinstance instead of alloc!"); returnNil;}+ (ID)New{ return[Self alloc];}+ (ID) allocwithzone: (struct _nszone *) zone{@synchronized (self) {if (s_sharedinstance = = nil) { S_sharedinstance = [Super Allocwithzone:zone]; return s_sharedinstance; }} return nil;}-(ID) Copywithzone: (Nszone *) zone{Nsassert (1==0,@"[Prautologinview]copy is not permitted!"); [Self retain]; returnSelf ;}-(ID) Mutablecopywithzone: (Nszone *) zone{return[self copywithzone:zone];}
The point to note here is that when Allocwithzone is called by default, even if you do not explicitly call alloc or allocwithzone, you need to overload
The perfect singleton mode under "original" iOS, verified