单例的定义
在.h 文件中定义:
+(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 文件中定义:
+ (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 { }
posted on 2017-04-10 17:25 Pierce-lph 阅读(143) 评论(0) 编辑 收藏 举报