学科:IOS/KB/Singletons

来自维基学院

一个线程安全的单例参考:

   + (MyClass *)sharedInstance {
   	static MyClass *sharedInstance = nil;
   
   	if(sharedInstance == nil) {
   		@synchronized(self) {
   			if (sharedInstance == nil)
   			sharedInstance = [[self alloc] init];
   		}
   	}
   
   	return sharedInstance;
   }

参考[编辑 | 编辑源代码]