"Pro Objective-C Design Patterns for iOS" subclassing Singleton confusion -
i did not understand question, test following 2 methods create subclass instance, results running well. example singletonson: singleton, sub-class without modification, when call [singletonson sharedinstance] or [singletonson alloc] returns instance singletonson instead of singleton. contrary results original content in book, original said:if not modified subclass singleton, returns instance of singleton.
+(singleton *) sharedinstance { if(sharedsingleton==nil) { sharedsingleton=[[super allocwithzone:null] init]; } return sharedsingleton; } +(singleton *) sharedinstance { if(sharedsingleton==nil) { sharedsingleton=[nsallocateobject([self class],0,null) init]; } return sharedsingleton; }
i chinese student, english not good, hope forgive me. looking forward answer.
well, i'd remove "pro", because code isn't thread safe @ all. here accepted pattern create singleton:
+(singleton *)sharedsingleton { static dispatch_once_t once; static singleton *sharedsingleton; dispatch_once(&once, ^{ sharedsingleton = [[self alloc] init]; }); return sharedsingleton; }
Comments
Post a Comment