#import@interface Singleton : NSObject + (Singleton*)sharedManager;
@end
#import "Singleton.h"static Singleton* sharedGizmoManager = nil;//static 静态实例是关键@implementation Singleton+ (Singleton*)sharedManager{ @synchronized(self){ if(sharedGizmoManager == nil) { [[self alloc] init];// assignment not done here } } return sharedGizmoManager;}//唯一一次alloc单例,之后均返回nil+ (id)allocWithZone:(NSZone*)zone{ @synchronized(self){ if(sharedGizmoManager == nil) { sharedGizmoManager = [super allocWithZone:zone]; return sharedGizmoManager; // assignment and return on first allocation } } return nil; //on subsequent allocation attempts return nil}//copy返回单例本身- (id)copyWithZone:(NSZone*)zone{ return self;}//retain返回单例本身- (id)retain{ return self;}//引用计数总是为1- (unsigned)retainCount{ return UINT_MAX; //denotes an object that cannot be released}//release不做任何处理- (oneway void)release{ }//autorelease返回单例本身- (id)autorelease{ return self;}@end