在.h 文件中定義:spa
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead"))); -(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead"))); +(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));
在.m 文件中定義:code
+ (instancetype)sharedInstance { static id shared = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shared = [[super alloc] initUniqueInstance]; }); return shared; } -(instancetype)initUniqueInstance { self = [super init]; if (self) { [self customInit]; } return self; } - (void)customInit { }