快速创建单例

 1 // .h文件中使用:
 2 #define PYJSingletonH(name) + (instancetype)shared##name;
 3 
 4 // .m文件中使用:
 5 #define PYJSingletonM(name) \
 6 static id _instance; \
 7 \
 8 + (instancetype)allocWithZone:(struct _NSZone *)zone \
 9 { \
10 static dispatch_once_t onceToken; \
11 dispatch_once(&onceToken, ^{ \
12 _instance = [super allocWithZone:zone]; \
13 }); \
14 return _instance; \
15 } \
16 \
17 + (instancetype)shared##name \
18 { \
19 static dispatch_once_t onceToken; \
20 dispatch_once(&onceToken, ^{ \
21 _instance = [[self alloc] init]; \
22 }); \
23 return _instance; \
24 } \
25 \
26 - (id)copyWithZone:(NSZone *)zone \
27 { \
28 return _instance; \
29 }

 

posted @ 2016-09-24 11:03  PengYunjing  阅读(250)  评论(0)    收藏  举报